TDZ is the debug reason let, const, and class throw before initialization even though they are hoisted. Strong answers separate TDZ from undeclared identifiers and show the startup bugs this creates in real modules.
Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
Explain Hoisting and the Temporal Dead Zone (TDZ)Frontend interview answer
This JavaScript interview question tests whether you can explain JavaScript TDZ and hoisting: Cannot access before initialization explained, connect it to production trade-offs, and handle common follow-up questions.
- JavaScript TDZ and hoisting: Cannot access before initialization explained explanation without falling back to memorized docs wording
- Hoisting and Temporal Dead Zone reasoning, edge cases, and production failure modes
- How you would answer the most likely JavaScript interview follow-up
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 |
|---|---|---|
|
| Hoisted and initialized with undefined. |
| ReferenceError | Hoisted but uninitialized (TDZ). |
| ReferenceError | Same TDZ rule; also must be initialized at declaration. |
console.log(a); // undefined
var a = 1;
console.log(b); // ReferenceError
let b = 2;
Function nuance
sayHi(); // works
function sayHi() { console.log('hi'); }
sayBye(); // ReferenceError (TDZ, because const)
const sayBye = () => console.log('bye');
Production bug to recognize
A startup file reads config before the later const config = ... line executes, or a class is used before initialization. That is TDZ, not “JavaScript forgot to hoist.”
{
// console.log(typeof featureFlag); // ReferenceError inside TDZ
const featureFlag = true;
}
// console.log(Service); // ReferenceError
class Service {}
Important follow-up
- Undeclared identifier: name lookup fails.
- TDZ: name exists, but initialization has not happened yet.
typeofis safe for undeclared globals, but not for TDZ variables.
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."
Hoisting books a variable in advance. TDZ is the "do not enter yet" sign until execution reaches that line.
- Hoisting happens before execution.
varreads as undefined before assignment.let/constthrow before declaration because of TDZ.- Function declarations are callable early; function expressions follow variable rules.
Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.