SKU // 100

JavaScript undefined

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.

// Where undefined Comes From

// 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 undefined undefined undefined 1 undefined undefined
Debugging Habit
When something is unexpectedly 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?

// Empty Is Not undefined

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);
false string 0

// Emptying a Variable

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);
undefined undefined null object

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.

W3Schools error
Its "You Can Empty an Object" example declares const person and then runs person = undefined;. That line is TypeError: Assignment to constant variable. Use let for any variable you plan to empty.

// undefined vs null

Both mean "no value," but they signal different intentions:

undefined null
MeaningNot set yet / does not existDeliberately empty
Who sets itUsually JavaScriptUsually your code
typeof"undefined""object"
In mathNaN (undefined + 1)Counts as 0 (null + 1 is 1)
In JSONProperty droppedKept as null
null == undefined    // true   loosely equal
null === undefined   // false  different types

JSON.stringify({ a: undefined, b: null })   // '{"b":null}'
Convention
Let JavaScript produce 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.

// Checking for undefined

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
Not a Truthiness Check
if (!value) is also true for 0, "", and false. If 0 or an empty string are valid values, check for undefined / null specifically.

// Handling It Gracefully

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"
0 dark undefined Guest

// Default Parameters Only Trigger on undefined

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
Hi, friend Hi, friend Hi, null

// The Classic Crash

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')
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.

// Quick Check

What does this log?

const settings = { volume: 0 };
console.log(settings.volume || 50, settings.volume ?? 50);
Show answer
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?

  1. Calling a function that has no return
  2. Reading a property that does not exist
  3. JSON.parse("null")
  4. Reading a let variable that was declared but never assigned
Show answer
C. It gives null: 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?

Show answer
user.address. The message names the property it was trying to read (city), so the value before it is the one that was undefined.

// Try It

  1. Click Open Playground. Produce undefined in all six ways from the first example
  2. Compare undefined + 1 with null + 1
  3. Stringify an object with one undefined property and one null property
  4. Trigger the "Cannot read properties of undefined" error on purpose, then fix it with ?. and ??
// Video Walkthroughs
Last updated: Oct 10, 2026, 1:48 AM ET