These two sound alike but mean completely different things. Confusing them is one of the most common beginner mistakes.
Save
Complete lesson & earn 250 PX
EXERCISE
1When JavaScript says a variable is undefined, it does not mean the variable does not exist. It means memory was reserved, but nobody filled it with a value yet.
Save
This is one of the most misunderstood concepts in . Many beginners think "undefined" means "does not exist". It does not. It means "exists, but empty".
undefined is a real value:
var box;
console.log(box); // undefined
console.log(typeof box); // "undefined"
// JavaScript created a slot for "box" in memory
// But you never assigned anything to it
// So the slot contains the placeholder: undefined
undefined is automatic:
var name;
// Right now, name === undefined
name = "Layla";
// Now name === "Layla"
// undefined was the AUTOMATIC placeholder
// It was never an error, never a crash
// Just a signal: "this variable exists but has no value yet"
undefined is a type:
var x;
console.log(x); // undefined
console.log(typeof x); // "undefined"
// typeof returns "undefined" as a string
// undefined is both a value AND a type in JavaScript
// It is one of the 7 primitive types
> Key Insight: JavaScript is a loosely typed language. A variable can hold a number, then a string, then undefined. The engine does not care. That flexibility is powerful but also dangerous: you can accidentally change the type of a variable without realizing it.
undefined means memory was allocated but no value assigned yet. not defined means the variable was never declared anywhere. typeof is the safe way to check.