Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7443513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:20:29+00:00 2026-05-29T11:20:29+00:00

Trim leading and trailing spaces from name undefined. trimName accepts a person as an

  • 0

Trim leading and trailing spaces from name “undefined”.

trimName accepts a person as an argument. Person will always be an object. Return undefined if name is not defined. Otherwise, return a trimmed name.

var person = {};
var trimmedName;
person.name;
function trimName(person) {
  // If you do not set this variable to a value, it will be
  // undefined.
  // Do a check here to see if a person's name is defined.
  if (person.name = 'undefined') {
    return 'undefined';
  }
  else {
    trimmedName = person.name.trim();
    return trimmedName;
  }
}
trimName(' sam ');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:20:30+00:00Added an answer on May 29, 2026 at 11:20 am

    Ï don’t see a question there… but I see issues with the code:

    • You are using the assignment operator = where you should use the comparison operator ==.
    • Comparing a string to the string 'undefined' is not the way to check if an property is undefined.
    • The trim method only exists in the latest version (9) of IE.
    • You are calling the function with a string instead of an object.

    Code:

    function trimName(person) {
      var trimmed;
      if (typeof person.name == 'undefined') {
        trimmed = 'undefined';
      } else {
        trimmed = person.name.replace(/(^\s+|\s+$)/g, '');
      }
      return trimmed;
    }
    
    var trimmedName = trimName({ name: ' sam ' });
    

    Demo: http://jsfiddle.net/Guffa/vCkSq/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to trim the leading and trailing spaces from a multiline string. I've
How to configure JAXB unmarshaller so it will trim leading and trailing whitespaces from
I need to trim all the trailing white spaces from the text elements of
Is there any way to trim (remove leading/trailing spaces) the input entered by a
Possible Duplicate: Strip Leading and Trailing Spaces From Java String When I import data
I want to trim the value (strip leading and trailing spaces) and make first
Microsoft Doc says about String.Trim Removes all leading and trailing white-space characters from the
Using the following function: // remove multiple, leading or trailing spaces function trim(s) {
Is there a template filter in django that will trim any leading or trailing
What's the simplest way to make sure models trim leading and trailing white space

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.