LESSON 04 / 16·2 min readFoundation

Hoisting & the temporal dead zone

Try code in the playground
01 Understand02 Recall03 Prepare

Understand the concept

Hoisting is JavaScript's behavior of processing declarations before executing the code in a scope. Because of this, a function declaration can usually be called before its line appears, while variables behave differently depending on how they are declared.

The Temporal Dead Zone (TDZ) is the period from entering a scope until a let, const or class binding is initialized. The binding exists in that scope but cannot be accessed yet; trying to read it throws a ReferenceError.

Important ideas

  • JavaScript sets up declarations before running that scope.

  • let and const cannot be accessed before initialization; this period is the TDZ.

  • console.log(x); let x = 1; throws ReferenceError.

  • var starts with undefined; let and const exist in scope but cannot be read until initialized.

  • A function expression follows the rules of the variable holding it; assigning an arrow function to const does not make it callable early.

  • Even typeof throws for a binding in its TDZ, unlike typeof on an undeclared name.

  • Declarations are not literally moved in the source code; this describes how bindings are prepared.

Work through the example

JavaScript
console.log(a); // undefined
var a = 10;

try {
  console.log(b); // Throws ReferenceError: b is in its TDZ.
  let b = 20;
} catch (error) {
  console.log(error.name); // ReferenceError
}
  • var a is initialized before the first log, but its assignment to 10 happens later.
  • b is known in its block, but reading it before let b = 20 is not allowed.
PAUSE & RECALL

Can you explain it without the notes?

What are hoisting and the TDZ (Temporal Dead Zone)?

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Hoisting is JavaScript's behavior of processing declarations before executing the code in a scope. Because of this, a function declaration can usually be called before its line appears, while variables behave differently depending on how they are declared.

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

  • Explain both declaration setup and initialization. Saying only “variables move to the top” misses the important difference.
Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Are objects passed by reference?

Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “What are hoisting and the TDZ (Temporal Dead Zone)?”. Examples are study fragments; framework behavior can depend on your version.