undefined
Introduction
undefined is the primitive value JavaScript uses when something has not been given a value: an uninitialised variable, a missing property, an omitted argument, or a function that returns nothing.
Where null often means deliberate emptiness, undefined usually means the value is missing by default.
Why This Matters
undefined shows up everywhere in real debugging: optional config, missing API fields, default parameters, array holes, and forgotten return statements. Interviewers use it to test whether you can distinguish language defaults from intentional application state.
Theory
Where undefined comes from
| Source | Example | Result |
|---|---|---|
| Declared but not assigned | let x; | x is undefined. |
| Missing property | obj.missing | undefined. |
| Omitted argument | fn() for parameter x | x is undefined. |
| No explicit return | function f() {} | f() returns undefined. |
void operator | void 0 | Always evaluates to undefined. |
undefined vs undeclared
A declared variable can have the value undefined. An undeclared name does not exist in the scope chain and direct access throws ReferenceError. typeof undeclaredName is the safe exception: it returns 'undefined'.
Defaults
Default parameters and nullish coalescing often treat undefined as missing. function greet(name = 'friend') uses the default when the argument is omitted or explicitly undefined, but not when it is null.
void 0
Historically, void 0 was used as a guaranteed way to produce undefined because old JavaScript allowed the global undefined property to be overwritten. Modern code can use undefined directly, but void 0 still appears in minified or legacy snippets.
Visual Diagrams
let x; -> undefined
({}).missing -> undefined
function f() {} f() -> undefined
void 0 -> undefined`undefined` is the default value for several forms of missingness.
Code Examples
Default parameters use undefined
The default runs for omitted or undefined, but not for null.
Declared undefined vs undeclared
A variable can exist with value undefined; an undeclared name is not in scope.
Output Prediction
Predict the output #1
1let value;2function noReturn() {}3function show(arg) {4 console.log(arg);5}6 7console.log(value);8console.log({}.missing);9console.log(noReturn());10show();11console.log(void 0 === undefined);Interview Questions
1What is the difference between `undefined`, `null`, and an undeclared variable?
undefined is a real primitive value that usually means missing by default. null is a real primitive value used for intentional absence. An undeclared variable is not bound in the scope chain; direct access throws ReferenceError, although typeof undeclaredName safely returns 'undefined'.
Follow-ups
- When do default parameters apply?
- What does `void 0` do?
Quiz
1. Which expression does NOT naturally produce `undefined`?
Summary
- `undefined` means a value is missing, uninitialised, omitted, or not returned.
- A declared variable can hold `undefined`; an undeclared name is a ReferenceError on direct access.
- Default parameters apply to omitted or explicitly `undefined` arguments, not to `null`.
- `void 0` is a legacy-safe way to produce `undefined`.
Cheat Sheet
Common sources: let x;, missing property, omitted argument, no return, void 0.
Declared vs undeclared: declared can equal undefined; undeclared direct access throws.
Defaults: parameter defaults trigger on undefined, not null.
Nullish checks: value == null matches both null and undefined; use only when intentional.