Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Types  ›  Lesson

undefined and null

undefined 12 minundefined is the value the engine gives to anything never set, while null is written by a programmer to mean deliberately empty.

undefined and null

In short: JavaScript has two different "empty" values, and they mean different things. undefined is what the engine gives you when something was never set — a missing property, an unassigned variable, a function that falls off the end without a return. null is what a person writes on purpose to say "this is deliberately empty." Mixing the two up leads to checks that look right but miss half the cases.

Two panels: on the left the engine shrugs at song.year, which was never written, giving undefined; on the right a person deliberately writes album: null, giving null

A song with no album yet

A song object can have a field a person filled in as empty, and a field nobody ever added at all — they read very differently:

javascript

song.album is null: someone decided this song has no album and recorded that fact. song.year is undefined: the property was never added, so the engine has nothing to report. Same "empty" feeling, opposite origin.

Reading an incomplete songStep 1 of 4
  1. The data

    const song = { title: "Blue Hour", album: null };

A function that forgets to return

Every function returns something, even when its body doesn't say so:

javascript

logPlay never has a return, so calling it gives undefined back — the engine's default answer when you ask a function for a value it didn't promise one. This is the single most common way undefined shows up by accident: a forgotten return.

Reading a property of nothing

The real danger isn't the empty value itself — it's reaching into one:

javascript

song.year is undefined, and undefined has no properties, so .length throws a TypeError. The fix is to check first: song.year ? song.year.length : 0, or the shorter song.year?.length, which returns undefined instead of throwing when the left side is missing.

One-liners worth knowing: null == undefined is true (loose equality treats them as the same kind of empty), but null === undefined is false (different types). typeof null is "object" — a decades-old leftover bug kept for compatibility — while typeof undefined is "undefined".

Quick practice

quick drill

What does let next; console.log(next); log?

quick drill

A form's JSON has no discount field at all. What does order.discount give?

quick drill

A developer wants to record "this user has no referrer, and we checked" — which value should they write?

quick drill

What do null == undefined and null === undefined evaluate to?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTwo kinds of empty

Explain the difference between song.album being null and song.album being undefined, give one way each can happen, and say what happens with song.album.length in both cases.