Interview answer drill

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

HighIntermediateJavascript
Interview focus

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
Practice more JavaScript interview questions
Interview quick answer

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.

Full interview answer

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

var a

undefined

Hoisted and initialized with undefined.

let b

ReferenceError

Hoisted but uninitialized (TDZ).

const c

ReferenceError

Same TDZ rule; also must be initialized at declaration.

Behavior difference that causes most interview bugs.
JAVASCRIPT
console.log(a); // undefined
var a = 1;

console.log(b); // ReferenceError
let b = 2;
                  

Function nuance

JAVASCRIPT
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.”

JAVASCRIPT
{
  // 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.
  • typeof is 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."

Still so complicated?

Hoisting books a variable in advance. TDZ is the "do not enter yet" sign until execution reaches that line.

Summary
  • Hoisting happens before execution.
  • var reads as undefined before assignment.
  • let/const throw before declaration because of TDZ.
  • Function declarations are callable early; function expressions follow variable rules.
Similar questions
Guides
Preparing for interviews?

Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.