How to check for an undefined variable in Javascript

There are a number of ways to check if a variable is undefined, and not all of these methods are created equal. Some of them work half of the time; some of them are just plain wrong, and one method stands above all others in usefulness. In this (somewhat lengthy) article, you’ll find out which is which– and why! If you just want the “right” way to check if a variable is undefined, scroll right to the bottom, where I show you the proper method to do this sort of check.

Comparing against undefined

Naturally, the first method you’d think of to test if a variable is undefined would be to compare it against the undefined keyword, like this:

https://gist.github.com/2515177

However, while this would appear to be correct and indeed would work at first, it has two pitfalls.

You can assign a variable to undefined

That’s right, you can assign a variable to undefined! This can cause undesired and often very hard to catch problems. Observe:

https://gist.github.com/2515193

Despite what it looks like, this if statement will evaluate to true! Even though we’re checking to see if myVar is undefined, (and it clearly is defined in the above code) this if statement will evaluate to true. Why? Because we’ve defined undefined to also equal true, so we’re actually checking if “true == true”, which is obviously… true. This is one good reason not to compare directly against undefined– now let’s see the next one.

You can’t compare an undeclared variable against undefined

Let’s say we have the following code, in which we check to see if the variable someVar has been declared at all:

https://gist.github.com/2515204

If someVar hasn’t been declared, the following code should execute; but it doesn’t. Why? Because you can’t compare against an undeclared variable. Instead, the above code will throw an error. So clearly, comparing against undefined ain’t the way to go.

Using if(someVar) to check for defined-ness

This is a decent method; it looks (note the emphasis on “looks”) short and very elegant. Let’s have a look:

https://gist.github.com/2515217

Looks great, right? However, we have a problem. Let’s try checking the defined-ness of an undeclared variable, as opposed to an undefined variable:

https://gist.github.com/2515221

The above code will throw also an error– why? Because you can’t compare against an undeclared variable and this will also throw an error, just as checking against undefined did. If you’re 100% sure that your variable will always be declared before you check it, this check will work well. If, however, you’re not sure that your variable will be declared or not, then see the next (and last, but not least) check.

The end-all be-all of “is defined” checks

I’ve saved for last a final check that is a completely failproof method for checking if a variable is undefined. It works in all cases and scenarios you could possibly dream up, although it does look somewhat ugly. I give you: the typeof == undefined method:

https://gist.github.com/2515308

The above code evaluates to true, as expected. This is nothing big, though. The key thing that seperates this check from the others is the way it handles variables that are undeclared. Watch this:

https://gist.github.com/2515316

Unlike the other two methods, checking the type of an undeclared variable will not result in an error, because you’re not directly trying to access its value. This makes using typeof handy when you’re not sure that the variable you’re checking is undefined.

In summa

Although I’ve stated that typeof == “undefined” is the best way to check if a variable is undefined or not, that doesn’t mean you can’t use the other two methods. if(someVar) is pretty useful and if you’re sure that the variables you’re checking are always defined, it would be best to use this one. Keep in mind that comparing against an undeclared variable will always result in an error with this method, though.