Skip to the workspace
Pebellslearn by building
Data Types  ›  Lesson

undefined and null

undefined 12 min

javascript / undefined and null

Explore each scene at your own pace.

01 · undefined and null

undefined and null

undefined and null

undefined means no value has been supplied in a particular place. null is a value commonly used to represent an explicitly empty choice.

An unfilled variable and a deliberately cleared selection can both be empty, for different reasons.

See the mechanism

Walk through the modelundefined and nullAuthored snapshots of the example; use Run below to test the code.
draftundefined
The declaration has not supplied a starting value.
1 of 3

Follow one example

javascript

let draft declares a name without supplying a value, so reading it produces undefined. selectedArticle is assigned null deliberately. Neither word is a quoted string, and neither is the number 0.

Predict, change and check

Predict: If you assign "First draft" to draft before printing, which output line changes?

Test your explanation: Assign draft = "First draft" after its declaration. Then clear it with draft = null and compare that result with never assigning it.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Missing, empty and present-but-falsy

undefined commonly appears when a binding has no supplied value, an argument is omitted, or a property is absent. null is commonly an explicit empty value chosen by an API contract. These conventions help explain intent, but JavaScript does not enforce one application-wide meaning for them.

Use value ?? fallback when only null and undefined should trigger the fallback. value || fallback also replaces 0, false and empty text, which may corrupt valid input. A missing property and an own property containing undefined can read the same way; test ownership separately when supplied-field presence matters.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.