Explain Hoisting and the Temporal Dead Zone (TDZ)

HighIntermediateJavascript
Quick Answer

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.

Answer

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

var a

undefined

Hoisted and initialized with undefined.

let b

ReferenceError

Hoisted but uninitialized (TDZ).

const c

ReferenceError

Same TDZ rule; also must be initialized at declaration.

Behavior difference that causes most interview bugs.
JAVASCRIPT
console.log(a); // undefined
var a = 1;

console.log(b); // ReferenceError
let b = 2;
                  

Function nuance

JAVASCRIPT
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."

Still so complicated?

Hoisting books a variable in advance. TDZ is the "do not enter yet" sign until execution reaches that line.

Summary
  • Hoisting happens before execution.
  • var reads as undefined before assignment.
  • let/const throw before declaration because of TDZ.
  • Function declarations are callable early; function expressions follow variable rules.
Similar questions
Guides
Preparing for interviews?

Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.