TDZ Timeline and Duration
You'll learn: Exactly when the Temporal Dead Zone begins and ends for let and const variables.
The TDZ Lifecycle
The Temporal Dead Zone (TDZ) isn't just a concept—it has a specific lifespan. It starts at the very top of the block where a let or const variable is declared, and it ends the moment JavaScript executes the line containing that variable's declaration.
Think of the TDZ like a "construction zone" on a road. The zone begins where the block starts (the entrance to the construction area), and it ends precisely where the variable is declared and initialized (where construction is complete and the road opens).
Timeline Breakdown
// Block starts here
{
// TDZ for myVar STARTS here (top of block)
// Still in TDZ - accessing myVar would throw ReferenceError
// Still in TDZ...
// Still in TDZ...
let myVar = 10; // TDZ for myVar ENDS here (declaration line)
// Safe to use myVar now
console.log(myVar); // 10
}
The TDZ duration is the space between the opening curly brace { and the line with let or const. Even if that's just one line, or fifty lines—the TDZ exists for that entire span.
Why This Matters
Understanding the timeline helps you avoid accessing variables too early. The variable technically exists (it's been hoisted), but it's locked in the TDZ until its declaration line executes.
Key Takeaway: The TDZ for a let or const variable begins at block entry and ends at the exact line where the variable is declared—accessing it anywhere in between throws a ReferenceError.