undefined
Holds the primitive value given to unassigned variables, missing properties and empty returns.
undefined- Added in JavaScript Baseline widely available since July 2015
Return value
undefinedThe primitive value undefined, the sole member of the Undefined type.
Errors
Assigning to the global undefined throws a TypeError in strict mode, because the property is non-writable and non-configurable.
Changelog
- PHP ES5The global undefined property became non-writable and non-configurable. Before that it could be reassigned, which is why older libraries wrapped code in a function with an unfilled undefined parameter.
See also
undefined is the value JavaScript supplies whenever nothing else was supplied: a declared variable with no initializer, a property that does not exist, an argument the caller omitted, a function that falls off its end without returning, and the result of the void operator.
undefined versus null
Both mean "no value", but they arrive differently. undefined is the engine's default; null is an absence a programmer wrote on purpose. They are loosely equal (null == undefined is true) but not strictly equal, which is why value == null is the standard idiom for "either kind of empty". Default parameters and ?? treat only undefined (and, for ??, null) as missing.
Missing versus present-but-undefined
Reading a property that was never set and one explicitly set to undefined both give you undefined. Use the in operator or Object.hasOwn() to tell them apart. The distinction is visible elsewhere: JSON.stringify() drops undefined-valued properties entirely, and Object.keys() still lists them.
Checking safely
For a declared variable, x === undefined is fine. For a name that may not be declared at all, only typeof x === "undefined" avoids a ReferenceError — and even that throws inside the temporal dead zone of a let or const. Since ES5 the global property cannot be reassigned, but it is not a reserved word and can still be shadowed by a local binding, which is why minifiers emit void 0 instead.
Example
When to use it
- Distinguishing an argument the caller omitted from one they deliberately passed as null
- Detecting a typo in a property name when a lookup returns nothing
- Deciding between == null and === undefined in a guard clause
- Understanding why a key vanishes from JSON.stringify() output
Frequently asked questions
What is the difference between null and undefined in JavaScript?
undefined is the value JavaScript assigns when nothing was provided, while null is an empty value a developer assigns on purpose. They are loosely equal (null == undefined is true) but not strictly equal, typeof null is "object" while typeof undefined is "undefined", and default parameters replace undefined but not null.
How do I check if a variable is undefined?
Use value === undefined for a variable you know is declared, and typeof value === "undefined" when the name might not exist at all, since typeof never throws a ReferenceError. To catch null as well, use the loose check value == null.
Why does JSON.stringify drop my undefined properties?
JSON has no undefined value, so JSON.stringify omits object properties whose value is undefined and converts undefined array elements to null. Use null instead of undefined when the key must survive serialisation.
More examples
Default parameters, null, and the ?? operator
Default parameters trigger only on undefined, so an explicit null slips past and reaches the body. The nullish coalescing operator is broader — it catches both null and undefined — but unlike || it leaves falsy values such as 0 and "" alone.
function greet(name = "stranger") {
return `hello ${name}`;
}
console.log(greet()); // default applies
console.log(greet(undefined)); // default applies too
console.log(greet(null)); // default skipped: "hello null"
console.log(undefined ?? "fallback"); // "fallback"
console.log(null ?? "fallback"); // "fallback"
console.log(0 ?? "fallback"); // 0, unlike ||
console.log(0 || "fallback"); // "fallback"Every way a value ends up undefined
Six unrelated situations all produce the same primitive, and the final line confirms they are the identical value. void 0 is just a shorter, unshadowable way of writing it, which is why minified bundles are full of it.
let unassigned;
function noReturn() {}
function takesTwo(a, b) { return b; }
const arr = [1, 2];
console.log(unassigned); // never assigned
console.log(noReturn()); // no return statement
console.log(takesTwo(1)); // argument omitted
console.log(arr[5]); // index past the end
console.log(({}).nope); // property does not exist
console.log(void 0); // the void operator
console.log([undefined === void 0, unassigned === noReturn()]);typeof is the only safe check for an undeclared name
Reading a name that was never declared throws a ReferenceError, while typeof on the same name quietly returns "undefined". The exception is a let or const binding used before its declaration line: it is in the temporal dead zone, and even typeof throws there.
console.log(typeof neverDeclared === "undefined"); // true, no error
try {
console.log(neverDeclared);
} catch (err) {
console.log(err.constructor.name, "-", err.message);
}
try {
console.log(typeof tdzVariable); // still throws: temporal dead zone
} catch (err) {
console.log("TDZ:", err.constructor.name);
}
let tdzVariable = 1;
Discussion