Temporal Dead Zone
The ReferenceError That Saves Your Bugs
Scenario
The Temporal Dead Zone sounds like a Christopher Nolan movie. It's actually one of the best features
letandconstgave us.It's the window of time between when a
let/constvariable enters scope and when its declaration line actually runs. During that window, the variable exists in memory - but touching it throws a ReferenceError.Why would the language designers do this on purpose? Because
var's "silentundefined" behavior hid bugs for YEARS. TDZ makes those bugs loud and immediate.
The No-Touch Window
The Temporal Dead Zone is the period between when a let/const variable enters scope and when it's actually declared in the code.
During this window, the variable exists in memory but is uninitialized. Accessing it throws a ReferenceError.
Code Example
{
// TDZ for `name` starts here
console.log(name); // ReferenceError: Cannot access 'name' before initialization
// TDZ continues...
let name = "Rakibul"; // TDZ ends here
console.log(name); // "Rakibul" - works fine now
}
Why Does TDZ Exist?
TDZ catches bugs early. With var, you'd get undefined silently - and spend hours debugging why your variable is undefined instead of the value you expected.
// BAD: var hides bugs
console.log(user); // undefined - no error, silent bug
var user = "Admin";
// GOOD: let catches the bug immediately
console.log(user); // ReferenceError - you know instantly
let user = "Admin";
TDZ in Function Parameters
// TDZ can bite you in default parameters too!
function greet(a = b, b = 2) {
console.log(a, b);
}
greet(); // ReferenceError: Cannot access 'b' before initialization
// Fix: reverse the order
function greet(b = 2, a = b) {
console.log(a, b); // 2, 2
}
TDZ with typeof
// var: typeof works even before declaration
console.log(typeof undeclaredVar); // "undefined" - no error
// let: typeof STILL throws in TDZ
console.log(typeof myLet); // ReferenceError
let myLet = 10;
This is one of the rare cases where typeof can throw an error.
Common Mistakes
- Assuming
typeof variableis always safe - inside the TDZ, eventypeofthrows aReferenceError. - Referencing a default parameter that appears later in the same parameter list - parameters are evaluated left-to-right, so the right-hand one is still in its TDZ.
- Expecting
let/constto silently beundefinedbefore their declaration - they are hoisted but uninitialized; reading them is an error, by design.
Interview Questions
Q: What is the Temporal Dead Zone?
The period between when a
let/constvariable is hoisted into its scope and when its declaration line executes. Accessing the variable during this window throwsReferenceError: Cannot access 'X' before initialization.
Q: Does typeof work safely with variables in the TDZ?
No. Unlike undeclared variables where
typeofreturns"undefined"safely, variables in the TDZ will causetypeofto throw aReferenceError. This is because the variable is known to exist (it's hoisted) but is explicitly uninitialized.
Q: How is TDZ different from var's hoisting behavior?
varis hoisted AND initialized toundefined, so reading it before its assignment givesundefinedsilently.let/constare hoisted but left uninitialized - reading them during the TDZ throws a ReferenceError, which surfaces bugs loudly instead of hiding them.
Q: Why was the TDZ added to the language?
To catch use-before-declaration bugs at runtime.
var's silentundefinedhid these bugs for years; TDZ makes them immediate, diagnosable errors.