Lesson 43 of 296
null vs undefined
What you will learn
- What each one means
- Where
undefinedcomes from - When to use
null - How to check for them safely
- Safe defaults and safe property access
- Differences in JSON and maths
On this page
Both mean "no value", but differently
undefined | null | |
|---|---|---|
| Meaning | "No value has been given" | "Empty on purpose" |
| Who sets it | Usually JavaScript itself | Usually the developer |
typeof | "undefined" | "object" (a historic bug) |
Where undefined comes from
You will meet undefined in these situations:
let a;
console.log(a); 1. variable created with no value
const user = { name: "Sam" };
console.log(user.email); 2. property that does not exist
function greet(name) {
console.log(name);
}
greet(); 3. missing argument
function doNothing() {}
console.log(doNothing()); 4. function with no return
const list = [1, 2];
console.log(list[5]); 5. array index with no itemWhen to use null
Use null when you want to say "there is intentionally nothing here", especially when the value may be filled in later:
let selectedProduct = null; nothing chosen yet
const user = { name: "Sam", deletedAt: null }; not deletedDatabases and APIs often return null for an empty field.
A common team rule: never set undefined yourself; use null for "empty on purpose".
Comparing them
null === undefined false (different types)
null == undefined true (special loose-equality rule)Checking safely
if (value === null) { } only null
if (value === undefined) { } only undefined
if (value == null) { } null OR undefined
if (value === null || value === undefined) { } the same, written outDo not use if (!value) when 0, "" or false are valid values: they are also falsy and would be treated as "missing".
Safe defaults: ??
The nullish coalescing operator ?? gives a default only for null or undefined:
const quantity = 0;
quantity || 1 1 (wrong: 0 was a real value)
quantity ?? 1 0 (correct: 0 is kept)More examples:
0 ?? 1 0
"" ?? "N/A" ""
null ?? "N/A" "N/A"
undefined ?? "N/A" "N/A"Safe property access: ?.
The optional chaining operator ?. stops and returns undefined if the value on its left is null or undefined, instead of throwing an error:
const user = null;
user.name TypeError: Cannot read properties of null
user?.name undefined
order?.customer?.address?.cityYou can combine both:
const city = order?.customer?.address?.city ?? "Unknown";Part 5 covers both operators in more detail.
Differences in maths
null + 1 1 (null becomes 0)
undefined + 1 NaN (undefined becomes NaN)Differences in JSON
JSON.stringify({ a: null, b: undefined }) '{"a":null}'Properties with undefined are removed when converting to JSON. null is kept. This matters when sending data to an API: to tell the server "clear this field", send null, not undefined.
Default parameters
Default parameters (Lesson 38) are used for undefined, not for null:
function greet(name = "Guest") { return name; }
greet(undefined) "Guest"
greet(null) nullCommon mistakes
- Using
typeof x === "object"to check for objects (null passes). - Using
if (!value)when 0 or empty string are valid. - Using
||for defaults instead of??. - Sending
undefinedto an API expectingnull.
How to say it in an interview
"undefined means a value has not been assigned, and JavaScript produces it for uninitialised variables, missing properties, missing arguments and functions without a return. null is an intentional empty value set by the developer. typeof null is 'object' due to a historic bug. They are loosely equal but not strictly equal. I use optional chaining for safe access and nullish coalescing for defaults, because it does not treat zero or empty string as missing."
Key words
| Word | Meaning |
|---|---|
| undefined | A value that has not been set |
| null | A value that is empty on purpose |
| Nullish | Either null or undefined |
?? | Nullish coalescing: default only for null or undefined |
?. | Optional chaining: safe access that returns undefined instead of throwing |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.