temporal dead zone
1. var vs let vs const
var
Function-scoped.
Can be redeclared and updated.
Hoisted to the top of the scope → initialized with
undefined.
console.log(a); // undefined (hoisted)
var a = 10;
let
Block-scoped.
Can be updated, but not redeclared in the same scope.
Hoisted but not initialized → stays in the Temporal Dead Zone (TDZ) until the declaration line.
console.log(b); // ❌ ReferenceError (TDZ)
let b = 20;
const
Block-scoped.
Must be declared + initialized at the same time.
Cannot be updated or redeclared.
Also lives in the TDZ before initialization.
console.log(c); // ❌ ReferenceError (TDZ)
const c = 30;
2. 🔥 What is the Temporal Dead Zone (TDZ)?
The time between hoisting (when memory is allocated) and initialization (actual value assigned).
For
letandconst, variables exist in memory but are in a “dead zone” → accessing them givesReferenceError.
👉 Why? To prevent using variables before they are safely initialized.
3. Deep Example
// Global Scope
console.log(a); // undefined
// console.log(b); // ReferenceError (TDZ)
// console.log(c); // ReferenceError (TDZ)
var a = 1;
let b = 2;
const c = 3;
function test() {
// TDZ for x, y, z starts here
// console.log(x, y, z); // ReferenceError for y, z
var x = "var inside";
let y = "let inside";
const z = "const inside";
console.log(x); // "var inside"
console.log(y); // "let inside"
console.log(z); // "const inside"
}
test();
4. Scope & Block Example
{
var v = "I am var";
let l = "I am let";
const c = "I am const";
}
console.log(v); // ✅ "I am var"
// console.log(l); // ❌ ReferenceError
// console.log(c); // ❌ ReferenceError
👉 var escapes out of the block → only function-scoped.
👉 let and const stay inside the block → block-scoped.
5. 🧠 Why TDZ Exists?
To make
letandconstsafer thanvar.If TDZ didn’t exist:
console.log(x); // would be undefined let x = 10;This could cause bugs because you might accidentally use an uninitialized variable.
TDZ forces you to declare before use.
6. Tree Diagram (Execution Context Example)
// Global LE
{
a: undefined (var)
b: uninitialized (TDZ)
c: uninitialized (TDZ)
}
Before execution:
ais hoisted →undefinedbandcare hoisted too but stay uninitialized (TDZ)
When JS reaches:
var a = 1;→ initializeslet b = 2;→ initializesconst c = 3;→ initializes