Facebook Pixel

How to Check for undefined in JavaScript

There are several ways to check for undefined. Here is which is correct, which is risky, and which to avoid.

How to Check for undefined in JavaScript

Checking for undefined seems simple, but there are several ways and most have caveats. Here is the correct approach.

The Correct Way: Strict Equality

if (x === undefined) { ... }

This checks both type and value. It is the canonical, safe way.

The Loose Way: == null

if (x == null) { ... } `` `null == undefined` is `true` (special case). This catches both `null` and `undefined` in one check. It is useful when you treat both as "no value." ### The Risky Way: `!x` ```js if (!x) { ... }

This catches undefined, but also null, 0, "", false, and NaN. Use it only when you genuinely want to catch all falsy values. Do not use it to check specifically for undefined.

The typeof Way (for Possibly-Undeclared Variables)

if (typeof x !== "undefined") { ... }

typeof does not throw for undeclared variables. This is the safe way to check if a variable exists at all (e.g., feature detection for a global API). For declared variables, === undefined is clearer.

The void 0 Way (for Old Code)

if (x === void 0) { ... }

void 0 always evaluates to undefined and cannot be shadowed. In ES5+ where undefined is non-writable, this is unnecessary, but you will still see it in minified code.

Checking Object Properties

const obj = { a: undefined }; // These all return true, but the property exists: "a" in obj; // true obj.hasOwnProperty("a"); // true // This returns undefined whether the property is missing or set to undefined: obj.a; // undefined obj.b; // undefined

Use in or hasOwnProperty if you need to distinguish "missing" from "set to undefined."

The Takeaway

Use === undefined for declared variables. Use == null to catch both undefined and null. Use typeof for possibly-undeclared variables. Use in or hasOwnProperty to distinguish a missing property from one set to undefined. Avoid !x for specific undefined checks.

Use strict equality: x === undefined. This checks both type and value. It is the canonical, safe way to check if a declared variable has the value undefined.

Because !x also catches null, 0, '', false, and NaN. If you only want to check for undefined, use === undefined. Use !x only when you want to catch all falsy values.

Use typeof. typeof an undeclared variable returns 'undefined' without throwing. For example: if (typeof someGlobal !== 'undefined') is safe for feature detection.

Use the in operator or hasOwnProperty. 'a' in obj returns true if the property exists (even if its value is undefined). obj.a returns undefined whether the property is missing or set to undefined.

void 0 always evaluates to undefined and cannot be shadowed. It is used in old code and minified output to safely get undefined when the global property might have been reassignable. In modern JS (ES5+), x === undefined is sufficient.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.