Skip to the solution
HardFrontend7 min read

What is the temporal dead zone?

465 views

The problem

Question presented to candidate: "What is the temporal dead zone, and how is it different from a variable simply being undefined?"

What a strong answer should cover:

  • The temporal dead zone (TDZ) is the span of code between entering a scope and the line where a let or const binding is actually declared, during which that binding exists but has not been initialized yet.
  • Accessing a variable while it is in its TDZ throws a real ReferenceError -- it is not the same as the variable being undefined.
  • var has no TDZ -- it is hoisted AND initialized to undefined immediately, so accessing it early just silently gives undefined instead of throwing.
  • Even typeof is not TDZ-safe -- typeof on a name that is declared later in the current scope with let/const throws, unlike typeof on a name that is not declared anywhere at all, which safely returns "undefined".
  • The TDZ is scope-local: it belongs to a specific binding in a specific scope, so an inner let of the same name as an outer variable creates its own TDZ that shadows the outer one, even before the inner declaration's line.
  • class declarations have a TDZ too, just like let and const -- they are not hoisted the way function declarations are.

Code / implementation expected: Yes -- a runnable snippet showing let/const throwing ReferenceError before their declaration line, contrasted with var's hoisted-undefined behavior.

  • temporal-dead-zone

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptlet/const throw in the TDZ; var silently returns undefined (run directly)
JavaScripttypeof is not TDZ-safe; class has a TDZ too (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 148 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

How do you combine Stage 3 decorators with `using` for auto-disposable resources?

Continue