Compile Ready
Module 2 · Variables & Data Types

undefined

Beginner6m read3m practice9m total
undefinedMissing ValuesDefaultsvoid

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

SourceExampleResult
Declared but not assignedlet x;x is undefined.
Missing propertyobj.missingundefined.
Omitted argumentfn() for parameter xx is undefined.
No explicit returnfunction f() {}f() returns undefined.
void operatorvoid 0Always 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

Missing-value pipeline
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.

Loading…

Declared undefined vs undeclared

A variable can exist with value undefined; an undeclared name is not in scope.

Loading…

Output Prediction

Predict the output #1

javascript
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'.

Asked at:GoogleAppleMicrosoft

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.