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.
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");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.
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");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.
console.log(typeof completelyUndeclared999 === "undefined");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.
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);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.
let z;
console.log("z", z == null, z === undefined, z === null);
let m = null;
console.log("m", m == null, m === undefined, m === null);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.
let declared;
console.log(declared === void 0);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.
console.log(undefined ?? "d");
console.log(null ?? "d");
console.log(0 ?? "d");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
=== undefinedfor 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
!valueas a javascript test for undefined unless you mean falsy. - Use
== nullor paired===checks when you need bothnullandundefined. - Use
??for defaults that should not trigger on0or"".
References
MDN pages for undefined, typeof, void, and nullish coalescing—core references for this javascript undefined check guide.
