All variable declarations are hoisted, but only var is initialized with undefined. Variables declared with let and const exist in the temporal dead zone until execution reaches their declaration, making them inaccessible beforehand.
Explain the difference in hoisting between `var`, `let`, and `const`
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
The Core Idea
All variable declarations are hoisted — meaning they’re recognized by JavaScript before execution. However, only var is initialized immediately with undefined. let and const are hoisted too, but they stay in the temporal dead zone (TDZ) until the actual declaration line.
Keyword | Hoisted? | Initialized Before Declaration? | Accessible Before Declaration? | Reassignable? | Redeclarable? |
|---|---|---|---|---|---|
| ✅ Yes | ✅ Initialized to | ✅ Yes (but value = undefined) | ✅ Yes | ✅ Yes |
| ✅ Yes | ❌ Not initialized (TDZ) | ❌ No — ReferenceError | ✅ Yes | ❌ No |
| ✅ Yes | ❌ Not initialized (TDZ) | ❌ No — ReferenceError | ❌ No (must initialize immediately) | ❌ No |
How It Works Under the Hood
When the JavaScript engine creates an execution context, it allocates memory for all declared variables:
varis assignedundefinedimmediately.letandconstare known but remain uninitialized until their declaration line is reached.
// Example 1: var
console.log(a); // undefined
var a = 10;
// Example 2: let
console.log(b); // ❌ ReferenceError (TDZ)
let b = 10;
// Example 3: const
console.log(c); // ❌ ReferenceError (TDZ)
const c = 10;
The Temporal Dead Zone (TDZ)
The TDZ is the time between entering a scope and the moment a let or const variable is declared. Accessing it during this period throws a ReferenceError. It helps prevent using variables before they're safely initialized.
{
// TDZ starts
console.log(x); // ❌ ReferenceError
let x = 5; // TDZ ends here
console.log(x); // ✅ 5
}
Imagine you’re checking into a hotel 🏨. Rooms (var, let, const) are reserved at check-in (hoisting), but only var gets its key immediately. let and const exist but you can’t enter until the receptionist actually hands over the key — that’s the temporal dead zone!
- All declarations are hoisted, but initialization differs.
var→ hoisted + initialized asundefined.let/const→ hoisted but uninitialized (TDZ).- Accessing
letorconstbefore declaration →ReferenceError. constalso requires immediate initialization.