Undefined
ELI5 — The Vibe Check
Undefined in JavaScript means a variable exists but has never been given a value. It is like a labeled empty box — the box is there, but nobody put anything in it. Null is 'empty on purpose'; undefined is 'empty because nobody got around to filling it yet'.
Real Talk
Undefined is a primitive value in JavaScript indicating a variable that has been declared but not yet assigned. It also occurs when accessing non-existent object properties, function parameters not passed by the caller, and functions that return nothing. It is distinct from null (intentional absence). TypeScript can catch many undefined-related bugs at compile time through strict null checks.
Show Me The Code
// How undefined appears:
let x; // declared but not assigned
console.log(x); // undefined
const obj = { name: 'Alice' };
console.log(obj.age); // undefined — property doesn't exist
function greet(name) {
console.log(name); // undefined if called as greet()
}
// Guard against undefined:
const age = obj.age ?? 18; // default if null or undefined
When You'll Hear This
"You're accessing a property that's undefined — check the object shape." / "TypeScript's strict mode catches undefined access before runtime."
Related Terms
NaN (Not a Number)
NaN means 'Not a Number' — it is what JavaScript gives you when math goes wrong in a weird way. Try to parse a word as a number and you get NaN.
Null
Null means 'intentionally nothing' — a programmer chose to say 'there is no value here'. It is a deliberate absence.
Runtime Error
A runtime error is one that only shows up when your program is actually running, not before.
Type
A type tells the computer what kind of thing a value is — is it a number, text, true/false, or a list?
TypeScript
TypeScript is JavaScript with a strict parent watching over it.