JS Built-ins
Variablesintermediate

Variables β€” let temporal dead zone (TDZ)

let has a Temporal Dead Zone

Like var, let is hoisted to the top of its block β€” but unlike var, accessing it before the declaration line throws a ReferenceError. The period between the start of the block and the declaration line is called the Temporal Dead Zone (TDZ).

Challenge: Write letTDZ() that:

  1. Uses eval to run a function that accesses x before declaring it with let
  2. Wraps the eval in try/catch
  3. Returns 'ReferenceError' if the error is caught, or 'no error' otherwise
πŸ’‘ Hints (1)
  • β€’ The eval trick forces a runtime TDZ error that can be caught