6

Is it possible to check for both null and undefined in javascript?

if(_var == null || _var == undefined) {

}
2
  • 1
    I would switch both expressions, but it should work. Commented May 18, 2010 at 18:48
  • 1
    If _var is undefined, the _var == undefined expression will never be reached, because _var == null does type coercion, as Pim noted below and undefined == null returns true. The _var == null will also be testing for undefined, however I believe relying on type coercion is not generally recommended. Commented May 18, 2010 at 19:07

5 Answers 5

6

In JavaScript (pre ECMAScript 5), undefined is not a constant, but a global variable, and therefore it is possible to change its value. Therefore it would be more reliable to use the typeof operator to check for undefined:

if (typeof _var === 'undefined') { }

In addition your expression would return a ReferenceError if the variable _var is not declared. However you would still be able to test it with the typeof operator as shown above.

Therefore, you may prefer to use the following:

if (typeof _var === 'undefined' || _var === null) { }
Sign up to request clarification or add additional context in comments.

3 Comments

Your first case is a little risky since _var might be set to some other falsy value than null.
@snowlord: True. I removed that part.
fortunately now on ECMAScript 5, undefined is a property of the global object which is non-writable, non-configurable (non-deletable) and non-enumerable :)
4

yes

However using the == operator it is not necesary. using foo == null will also be true of foo is undefined. Note however that undefined and null or not(!) the same. It is because that == does type coersion that foo == null is also true for foo is undefined.

1 Comment

+1, this is the shortest way to achieve it, and it is completely described in the spec: The Abstract Equality Comparison Algorithm (no worries about type coercion)... if (_var == null) { ... }
1
if (!_var) {
    // Code here.
}

This should work since both undefined and null are type coerced to false.

Of course there is the small problem if _var is actually false but it works since in most cases you would want to know if _var is not true and not an object.

1 Comment

Note that there are three other falsy values in JavaScript apart from false, null and undefined: An empty string "", 0, and NaN.
0

you can also use $defined function in mootools (and there must be an equivalent in jquery)

Comments

0
var valuea: number;
var valueb: number = null;

function check(x, name) {
    if (x == null) {
        console.log(name + ' == null');
    }

    if (x === null) {
        console.log(name + ' === null');
    }

    if (typeof x === 'undefined') {
        console.log(name + ' is undefined');
    }
}

check(a, 'a');
check(b, 'b');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.