Basics
null and undefined
undefined is "not put in yet"; null is "left empty on purpose".
Why it exists
A program constantly needs to say "there's no value": the user has no middle name, no product is selected, the server hasn't answered yet. If we used 0 or an empty string for that, we couldn't tell them apart from a real zero or real empty text. So there are special values. JavaScript itself puts undefined wherever there's no value yet, and a programmer writes null by hand to say explicitly: "this is empty, and that's fine".
Where it's used
- Optional fields:
middleName: nullon a form where there's no middle name. - Resetting a choice: after a dialog closes,
selectedProduct = null. - API responses and JSON: JSON has no
undefined, so "no value" is sent asnull. - Checking whether an argument was passed: a parameter that wasn't passed is
undefined.
Syntax
let x; // undefined
const y = null; // empty on purposeExamples
let middleName;
middleName; // → undefined
const selected = null;
selected; // → null
const cat = { name: "Byte" };
cat.age; // → undefinedfunction greet(name) {
return "Hello, " + name;
}
greet(); // → "Hello, undefined"undefined, and it gets glued in as textnull === undefined; // → false
null == undefined; // → true
Number(null); // → 0
Number(undefined); // → NaNJSON.stringify({ a: undefined, b: null }); // → '{"b":null}'null, but simply drops properties that are undefinedResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
undefined appears by itself: in a variable declared without a value, in a missing object property, in a parameter that wasn't passed, and as the result of a function without return. null never appears by itself — someone always wrote it.
Both values are "falsy", and both count as "no value" for the ?? and ?. operators. Loose comparison treats them as equal (null == undefined is true), strict comparison doesn't (null === undefined is false).
In calculations they behave differently: null turns into 0, and undefined into NaN ("not a number"). And typeof null returns "object" — a bug from 1995: the first engine stored null as an address made of zeros, and the type tag 000 meant "object". The fix was rejected because it would have broken existing sites.
Traps
How it differs from…
- The
??operator ?? substitutes a default value for exactly
nullandundefined:name ?? "Guest".
When to pick something else
Don't write undefined by hand: if you mean "empty", write null — then it's clear it was the programmer's decision, not a forgotten variable. And don't use null where there's a natural empty value: an empty list is [], not null, so length and loops work on it straight away.
Lessons where this appears
- Why is typeof null "object"?Why? · Chapter 1 · Variables and types
- The DoppelgängerBoss · Chapter 8 · Objects
- Unpacking parcelsTale · Chapter 9 · Destructuring, spread and rest
- The RepackerBoss · Chapter 9 · Destructuring, spread and rest
- Byte's cat showTale · Chapter 11 · Map and Set
- The case of the white screenInvestigation · Chapter 15 · Errors