Check if variable is not undefined because it's === true or has a value assigned

StackOverflow https://stackoverflow.com/questions/23303729

  •  09-07-2023
  •  | 
  •  

سؤال

Sometimes I see that it is common to do this on javascript in order to check if a variable is not undefined because it has a value assigned:

if(variable) {
  /* not undefined, do something */
}

However, I was asking to myself how can I check if variable is not undefined in the following situation:

Live Demo: http://jsfiddle.net/Vp8tN/1/

$(function() {

    var person = create('John');
    var isMarried = person.isMarried;

    console.log('isMarried: ' + isMarried);

    if(isMarried) {
        console.log('Do something!');
    }

});

function create(name) {
    var person = {};
    person.name = name;
    person.isMarried = getRandom();
    return person;
};

function getRandom() {
    var arr = [true, 'No', undefined];
    var rand = arr[Math.floor(Math.random() * arr.length)];
    return rand;
};

In this case isMarried variable can be true, 'No', or undefined.

So, my questions are...

1) How can I check if isMarried variable is NOT undefined because it is === true (equals true) or because it has a value assigned? (this last one happens when isMarried === 'No').

2) Is it possible to check this without using an extra if statement or condition?

3) What's the better approach for checking this?


In both cases described above (at number 1) I got inside the if statement. Check the output from browser console:

isMarried: true
Do something!
isMarried: undefined
isMarried: No
Do something!

PS. I am using jQuery just for testing, this question is NOT related to that framework! Thanks.

هل كانت مفيدة؟

المحلول

You have three options for a single equality test:

  1. Strict Equality. if (person.isMarried === true) or if (person.isMarried !== undefined). Check if a variable is explicitly equal to something (with no type conversions allowed).

  2. Loose equality. if (person.isMarried == true) with type conversions allowed.

  3. Any truthy/falsey value. if (person.isMarried). This will be satified if person.isMarried contains ANY truthy value. Even "no" would be a truthy value.

If you're trying to tell the difference between "no", false and undefined, you will likely have to use more than one comparison as those are all separate values of separate types.


If you only want to know if the variable has any value (e.g. is not undefined), then you can use the strict equality check and compare to the actual undefined value:

if (person.isMarried !== undefined) {
    // there is some value in person.isMarried though it could be anything
    // other than the undefined value
}

نصائح أخرى

  1. isMarried !== undefined
  2. Yes, see #1.
  3. 1

See this post. https://stackoverflow.com/a/3390426/1313439

EDIT: For reasons that jfriend00 pointed out in the comments to his answer, isMarried !== undefined is probably preferable to typeof isMarried !== 'undefined'.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top