How to Check for Undefined in JavaScript?

Learn about the best ways to check if a variable is undefined in JavaScript

In this article, we'll look at different ways of checking undefined values in JavaScript, along with a comparison to allow you to choose the best method for your project.

Checking undefined With Strict Equals

The primitive undefined value is represented by the global undefined property. With it, you could quite simply do:

let variable;

if (variable === undefined) {
    // ...
}

People argue against this, suggesting that the value of undefined can be reassigned by direct assignment. For example:

undefined = 'foo';

While the argument holds true for older browsers, this behavior was fixed in the ECMAScript 5 specification for undefined. Therefore, in modern browsers the value of undefined cannot be reassigned and should be safe to use.

If your concern is about supporting older browsers, even then this should be fine for the most part as there's no real need in a real-world application to reassign the value of undefined. However, you could also consider using other methods described in this article.

Please note that for variables that are not declared previously, using this method would throw a ReferenceError.

Checking undefined With void(0)

By definition, the void operator evaluates a given expression and returns undefined. Therefore, to check for undefined in a backward compatible manner we can simply do:

let variable;

if (variable === void(0)) {
    // ...
}

It is a common practice to use void(0), to obtain the undefined primitive value. This could be especially useful in cases where you believe window.undefined could be, or may have been, overwritten.

Both, void(0) and void 0, are equivalent.

Please note that for variables that are not declared previously, using this method would throw a ReferenceError.

Using typeof

Another good way of checking for undefined values is to use typeof operator which returns a string. For example:

if (typeof variable === 'undefined') {
    // ...
}

Both, typeof operand and typeof(operand), are equivalent.

Comparison: undefined vs. void(0) vs. typeof

Checking undefined With Strict Equals:

  • Simple and self-explanatory.
  • Isn't guaranteed, on old browsers, to always have the primitive undefined value, as it can be reassigned to any other value.

Checking undefined With void(0):

  • It is immutable;
  • It returns the primitive undefined value even if window.undefined was reassigned;
  • Works on all browsers;
  • Requires less characters (which can serve as a micro-optimization where size is a concern).
  • Isn't self-explanatory and could be hard to understand at first glance.

Using typeof:

  • It is immutable;
  • Works for variables that have not been declared;
  • It returns the type as a string which can be used to safely check for undefined values
  • It negates the need to explicitly check against the global undefined primitive;
  • It's easy to understand.

Hope you found this post useful. It was published (and was last revised ). Please show your love and support by sharing this post.