Hoisting and TDZ explain why let and const are registered before execution yet still throw before initialization. The common debug signal is ReferenceError: Cannot access X before initialization.
Explain Hoisting and the Temporal Dead Zone (TDZ)
Core idea
JavaScript has a setup phase before running lines of code. In that phase, declarations are registered (hoisting), but let/const stay unavailable until execution reaches their initialization line. That blocked window is the Temporal Dead Zone (TDZ), and it is the first thing to debug when you see Cannot access X before initialization.
Declaration | Before declaration read | Why |
|---|---|---|
|
| Hoisted and initialized with undefined. |
| ReferenceError | Hoisted but uninitialized (TDZ). |
| ReferenceError | Same TDZ rule; also must be initialized at declaration. |
console.log(a); // undefined
var a = 1;
console.log(b); // ReferenceError
let b = 2;
Function nuance
sayHi(); // works
function sayHi() { console.log('hi'); }
sayBye(); // ReferenceError (TDZ, because const)
const sayBye = () => console.log('bye');
Debugging rule of thumb
If you see ReferenceError: Cannot access X before initialization, think TDZ first, not typo first.
Interview tip
A high-signal answer is: "All are hoisted, but initialization differs. TDZ is why let/const fail before declaration."
Hoisting books a variable in advance. TDZ is the "do not enter yet" sign until execution reaches that line.
- Hoisting happens before execution.
varreads as undefined before assignment.let/constthrow before declaration because of TDZ.- Function declarations are callable early; function expressions follow variable rules.
Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.