Facebook Pixel

Temporal Dead Zone (TDZ) in JavaScript Explained

let and const live in the TDZ until their declaration line. Here is what that means and the errors it causes.

Temporal Dead Zone (TDZ) in JavaScript Explained

The Temporal Dead Zone (TDZ) is the period between entering a scope where a let or const variable is declared, and reaching the actual declaration line. During this period, accessing the variable throws a ReferenceError.

What the TDZ Is

{ console.log(x); // ReferenceError: Cannot access 'x' before initialization let x = 5; }

The moment the engine enters the block, it allocates memory for x (so x is hoisted), but x is in the TDZ until the let x = 5 line. Accessing it during the TDZ throws.

Why var Does Not Have a TDZ

var is initialized to undefined during the memory phase, so accessing it early returns undefined instead of throwing. let and const are not initialized until the declaration line, so the TDZ is the gap.

TDZ Is Per-Scope and Per-Variable

{ // y is in TDZ here console.log(y); // ReferenceError let y = 1; // z is NOT in TDZ here, because the let z line is above this point let z = 2; console.log(z); // 2 }

The TDZ ends when the engine reaches the declaration line, not when the block starts.

TDZ Applies to const Too

const has the same TDZ behavior as let, plus the rule that it must be initialized at declaration:

const a; // SyntaxError: Missing initializer in const declaration const a = 1; // OK

TDZ in Loops and Function Parameters

function foo(a = b, b = 2) {} // ReferenceError: Cannot access 'b' before initialization foo();

Default parameters are evaluated left to right. b is in the TDZ when a's default is evaluated.

for (let i = i; i < 5; i++) {} // ReferenceError

i in the initializer is in the TDZ until the declaration completes.

Why the TDZ Exists

To catch bugs. Pre-ES6, accessing a var early silently returned undefined, which caused subtle bugs. The TDZ forces you to declare before use, which is safer.

The Takeaway

The TDZ is the gap between entering a scope and reaching the let/const declaration line. Accessing the variable during this gap throws a ReferenceError. It applies to let, const, default parameters, and class declarations. It exists to force declare-before-use.

The period between entering a scope where a let or const variable is declared and reaching the declaration line. Accessing the variable during this period throws a ReferenceError.

No. var is initialized to undefined during the memory allocation phase, so accessing it early returns undefined instead of throwing. Only let and const have a TDZ.

ReferenceError: Cannot access 'x' before initialization. It is thrown when you try to read or use a let or const variable before its declaration line.

Yes. Default parameters are evaluated left to right. A parameter that references a later parameter throws ReferenceError because that later parameter is still in the TDZ.

To prevent bugs caused by accessing variables before declaration. Pre-ES6, var silently returned undefined in that case. The TDZ forces declare-before-use and surfaces the mistake as a clear ReferenceError.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.