Possible Duplicate:
Detecting an undefined object property in JavaScript

I wanted to check whether the variable is defined or not.

eg

alert( x );

Throws a not defined error
How can I catch this error?

link|improve this question

67% accept rate
3  
Looks like this is a granddaddy: stackoverflow.com/questions/1485840/… stackoverflow.com/questions/2647867/… - great gramps maybe this: stackoverflow.com/questions/27509/… – random Apr 19 '10 at 15:52
feedback

closed as exact duplicate by Saul, Robert Harvey Aug 24 at 22:56

This question covers exactly the same ground as earlier questions on this topic; its answers may be merged with another identical question. See the FAQ.

7 Answers

up vote 127 down vote accepted

in JavaScript null is an object. There's another value for things that don't exist, undefined. The DOM returns null for almost all cases where it fails to find some structure in the document, but in JavaScript itself undefined is the value used.

Second, no, they are not directly equivalent. If you really want to check for null, do:

if (null == yourvar) // with casting
if (null === yourvar) // without casting

If you want to check if a variable exist

if (typeof yourvar != 'undefined') // Any scope
if (window['varname'] != undefined) // Global scope
if (window['varname'] != void 0) // Old browsers

If you know the variable exists but don't know if there's any value stored in it:

if (undefined != yourvar)
if (void 0 != yourvar) // for older browsers

If you want to know if a member exists independent of whether it has been assigned a value or not:

if ('membername' in object) // With inheritance
if (object.hasOwnProperty('membername')) // Without inheritance

If you want to to know whether a variable autocasts to true:

if(variablename)

I probably forgot some method as well...

Source

link|improve this answer
10  
undefined is not a reserved word; you (or someone else's code) can do "undefined = 3" and that will break two of your tests. – Jason S May 13 '09 at 14:14
1  
"If you know the variable exists but don't know if there's any value stored in it" -- huh?! – Jason S May 13 '09 at 14:20
4  
I think he is referring to a variable declared that has not been assigned to. eg: var foo; // foo exists but does not have a value – Wally Lawless May 13 '09 at 14:29
1  
Hmmm... I just noticed the "source" link: this entire post is a direct quote from a mailing list, & should probably be edited to make that more clear, as the original author is not available to clarify. – Jason S May 13 '09 at 15:46
Also from what I can tell, that section "If you know the variable exists" has no difference from the previous section. The next section ("if you want to know if a member exists independent...") is correct. – Jason S May 13 '09 at 15:48
show 2 more comments
feedback

technically the proper solution is (I believe)

typeof x === "undefined"

You can sometimes get lazy and use

x == null

but that allows both an undefined variable x, and a variable x containing null, to return true.

link|improve this answer
feedback

The only way to truly test if a variable is undefined is to do the following. Remember, undefined is an object in Javascript.

if (typeof someVar === 'undefined') {
  // Your variable is undefined
}

Some of the other solutions in this thread will lead you to believe a variable is undefined even though it has been defined (with a value of NULL or 0, for instance).

link|improve this answer
this is the only answer that worked – Jason 2 days ago
feedback

I've often done:

function doSomething(variable)
{
    var undef;

    if(variable == undef)
    {
         alert('Hey moron, define this bad boy.');
    }
}
link|improve this answer
4  
Consider changing "==" to "===". If you call doSomething(null) you will also get the alert. Unless that's what you want. – Jason S May 13 '09 at 15:51
Yep. You have to decide if you want equivalent or exactly equal. Either case could have a use. – Joe Jul 7 at 15:41
feedback

Even easier and more shorthand version would be:

if( !x ){
   //undefined
}

OR

if( typeof x != "undefined" ){
    //do something since x is defined.
}
link|improve this answer
5  
the first code-piece can be incorrect if x is being set from a function call. like x = A(); if A doesnt return anything, it will return "undefined" by default. Doing a !x would be true which would be logically correct. However, if A() returns 0 then !x should be false as x=0. However in JS, !0 is also true. – Rajat Dec 30 '09 at 0:49
feedback

I've written an interesting blog post explaning this issue:

link|improve this answer
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.