JavaScript check if undefined (typeof, ===, void 0, nullish)

Tech reviewed: Deepak Prasad
JavaScript check if undefined (typeof, ===, void 0, nullish)

For a javascript undefined check or js check if undefined, you usually want the undefined primitive—uninitialized let/const, a missing return, or a missing object property. How to check undefined in javascript depends on whether the binding might be undeclared (use typeof) and whether null must be handled separately. Async APIs that surface undefined vs settled values often use await; when cloning defaults from JSON or configs without mutating shared slots, see clone object.

Tested on: Node.js v20.18.2. A short note after each snippet describes what you should see in the console.


Quick reference

typeof x === "undefined" is the only safe pattern when x might be undeclared; === undefined is clearest when the binding is known to exist.

Situation Typical pattern
Declared variable or parameter is undefined value === undefined
Binding might be undeclared typeof value === "undefined"
undefined or null value == null or two === checks
Default only for nullish value ?? defaultValue

Strict equality — check if undefined javascript (declared binding)

=== undefined is the clearest javascript test undefined when the identifier is declared in scope (let, const, var, or a parameter). It is false for null, 0, and "", so it answers “is this the undefined primitive?” only.

javascript
let x;
if (x === undefined) console.log("x is undefined");
let y = 0;
if (y === undefined) console.log("y is undefined");
else console.log("y is defined");
Output

You should see two lines: x is undefined, then y is defined.


typeof — javascript check for undefined (including undeclared)

typeof returns the string "undefined" for the undefined primitive and for identifiers that do not exist—without throwing (MDN: typeof). That makes it the safe javascript check if variable is undefined pattern when a name might never have been declared.

javascript
let a;
if (typeof a === "undefined") console.log("a is undefined");
let b = null;
if (typeof b === "undefined") console.log("b is undefined");
else console.log("b is defined");
Output

You should see two lines: a is undefined, then b is defined.

Here null is not treated as undefined: typeof null is "object", so check undefined js logic that must catch null needs value === null as well.


Undeclared name — js undefined check without ReferenceError

typeof completelyUndeclared999 === "undefined" is true even when no binding exists. Using completelyUndeclared999 === undefined would throw ReferenceError.

javascript
console.log(typeof completelyUndeclared999 === "undefined");
Output

You should see one line: true.


Why !value is not a javascript test for undefined

!u is true for undefined, but also for "", 0, false, NaN, and null—so it means “falsy”, not “undefined”. Prefer === undefined or typeof when you specifically mean the undefined primitive.

javascript
let u;
let v = 0;
let s = "";
let f = false;
console.log("u", u === undefined);
console.log("v", v === undefined);
console.log("s", s === undefined);
console.log("f", f === undefined);
Output

You should see four lines: u true, v false, s false, f false.


== null — undefined or null

value == null is true for both null and undefined (loose equality). It is a compact undefined check in javascript when you treat “no value” the same either way; use === when you must tell null and undefined apart.

javascript
let z;
console.log("z", z == null, z === undefined, z === null);
let m = null;
console.log("m", m == null, m === undefined, m === null);
Output

You should see two lines: z true true false, then m true false true.


void 0 — guaranteed undefined primitive

void 0 always evaluates to the undefined primitive. In modern code === undefined is usually enough for declared variables; void 0 still helps when you want a literal that cannot be shadowed by an old undefined reassignment pattern.

javascript
let declared;
console.log(declared === void 0);
Output

You should see one line: true.


Nullish coalescing (??) — javascript if not undefined defaults

?? substitutes only for null or undefined, unlike ||, which would replace 0 or "" as well—useful for javascript if not undefined defaulting while preserving other falsy values.

javascript
console.log(undefined ?? "d");
console.log(null ?? "d");
console.log(0 ?? "d");
Output

You should see three lines: d, d, 0.


Summary

Pick the test for what you mean: undeclared-safe typeof, primitive === undefined, combined nullish with == null, and defaults with ??—not bare !value.

  • Use === undefined for javascript check if undefined on declared bindings.
  • Use typeof x === "undefined" when the name might be undeclared or you want a safe js check undefined.
  • Do not use !value as a javascript test for undefined unless you mean falsy.
  • Use == null or paired === checks when you need both null and undefined.
  • Use ?? for defaults that should not trigger on 0 or "".

References

MDN pages for undefined, typeof, void, and nullish coalescing—core references for this javascript undefined check guide.


Frequently Asked Questions

1. javascript check if variable is undefined when it might never be declared?

Use typeof name === 'undefined'. That does not throw for an undeclared binding. Direct name === undefined throws ReferenceError if name was never declared in the current scope.

2. javascript if not undefined — what is the clearest test?

Use value !== undefined (and add !== null if you must reject null too). For defaults, prefer nullish coalescing (??) instead of || when only null/undefined should trigger a fallback.

3. typeof null is undefined in javascript?

No. typeof null is 'object' (historical). Use value === null for null and value === undefined or typeof value === 'undefined' for undefined.

4. Is !value a safe javascript undefined check?

No. !value is true for undefined, null, 0, NaN, '', and false. Use === undefined (or typeof) when you specifically mean the undefined primitive.

5. What is the difference between == null and === undefined?

value == null is true for both null and undefined. value === undefined is true only for undefined. Use explicit comparisons when you need to distinguish null.

6. Why use void 0 instead of undefined?

void 0 always evaluates to the undefined primitive. In very old scripts the global identifier undefined could be reassigned; void 0 avoids that. In modern code, === undefined is usually enough for declared variables.

7. js check undefined for a missing object property?

Reading a missing property yields undefined. Use obj.prop === undefined, Object.hasOwn / in for key existence, or optional chaining (obj?.prop) when the object itself might be nullish.
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital …

  • JavaScript
  • Web Design