undefined is JavaScript's way of saying "there is no value here yet." You will see it constantly, usually by accident. Knowing every place it comes from, and how it differs from null, makes a whole category of bugs easy to diagnose.
// 1. A variable declared but never assigned
let score;
score // undefined
// 2. A property that does not exist
const user = { name: "Ava" };
user.age // undefined
// 3. An array position with nothing in it
[10, 20][5] // undefined
// 4. A parameter that was not passed
function greet(name) { return name; }
greet() // undefined
// 5. A function with no return value
function log(x) { console.log(x); }
log(1) // undefined (after printing 1)
// 6. Searching and not finding
[1, 2, 3].find(n => n > 5) // undefined
undefined, walk this list: Was it assigned? Is the property name spelled right? Is the index in range? Was the argument passed? Did the function return?
An empty string is a real value with a real type. It only looks empty:
let text = "";
console.log(text === undefined);
console.log(typeof text, text.length);
You can clear a variable by assigning undefined or null. Both its value and its typeof change:
let car = "Volvo";
car = undefined;
console.log(car, typeof car);
let person = { firstName: "John", lastName: "Doe" };
person = null;
console.log(person, typeof person);
Prefer null for "I emptied this on purpose" (see the convention below). Either way, this only works with let: a const can never be reassigned.
const person and then runs person = undefined;. That line is TypeError: Assignment to constant variable. Use let for any variable you plan to empty.
Both mean "no value," but they signal different intentions:
| undefined | null | |
|---|---|---|
| Meaning | Not set yet / does not exist | Deliberately empty |
| Who sets it | Usually JavaScript | Usually your code |
| typeof | "undefined" | "object" |
| In math | NaN (undefined + 1) | Counts as 0 (null + 1 is 1) |
| In JSON | Property dropped | Kept as null |
null == undefined // true loosely equal
null === undefined // false different types
JSON.stringify({ a: undefined, b: null }) // '{"b":null}'
undefined; when you want to say "empty on purpose," use null. Then seeing null tells a reader someone chose it, and undefined suggests something was never set.
if (value === undefined) { } // exactly undefined
if (value == null) { } // undefined OR null: the one good use of ==
if (value === undefined || value === null) { } // same as above, spelled out
if (!value) is also true for 0, "", and false. If 0 or an empty string are valid values, check for undefined / null specifically.
const settings = { volume: 0 };
// ?? supplies a fallback only for undefined / null
settings.volume ?? 50 // 0 (kept: 0 is a real value)
settings.theme ?? "dark" // "dark"
// ?. stops safely when something along the way is undefined
settings.user?.name // undefined, no crash
settings.user?.name ?? "Guest" // "Guest"
function greet(name = "friend") {
return `Hi, ${name}`;
}
greet() // "Hi, friend"
greet(undefined) // "Hi, friend"
greet(null) // "Hi, null" null is a value you passed on purpose
The most common runtime error in JavaScript comes from reading a property of undefined:
const data = {};
data.user.name;
// TypeError: Cannot read properties of undefined (reading 'name')
Read the message closely: it tells you the property it was trying to read (name), so the thing before it (data.user) is the one that was undefined.
What does this log?
const settings = { volume: 0 };
console.log(settings.volume || 50, settings.volume ?? 50);
50 0. || replaces any falsy value, including 0. ?? only replaces undefined and null, so the real 0 is kept.Which of these does not give undefined?
returnJSON.parse("null")let variable that was declared but never assignednull: JSON has null but no undefined.You see TypeError: Cannot read properties of undefined (reading 'city') from the code user.address.city. Which part was undefined?
user.address. The message names the property it was trying to read (city), so the value before it is the one that was undefined.undefined in all six ways from the first exampleundefined + 1 with null + 1undefined property and one null property?. and ??