Skip to the workspace
Pebellslearn by building
All about Variables  ›  Lesson

Hoisting

Hoisting 12 min

javascript / Hoisting

Explore each scene at your own pace.

01 · Hoisting

Hoisting

Hoisting

Hoisting describes declaration behavior before execution reaches a declaration. var reads as undefined before assignment; let and const are inaccessible in their temporal dead zone.

Declarations do not literally move in your source file. Different declarations have different early-access rules.

Repair a reading-session panel that reads a value too early. Compare var, let and const, then fix the initialization order so the panel can render.

Opening your JavaScript project…

Reference and extra examples

Open this whenever you want a fuller explanation. Your project above is the main activity.

Reference model

These authored snapshots explain the example below; they are not a trace of your edited project.

Walk through the modelHoistingAuthored snapshots of the example; use Run below to test the code.
readsundefined
The binding is initialized before the later assignment.
1 of 3

Follow one example

javascript

The var binding exists with undefined before its assignment runs. After assignment it holds 2. Replacing var with let makes the first access throw a ReferenceError; execution does not continue to the assignment unless the error is handled. Ordinary function declarations can usually be called earlier in their scope; an arrow stored in const follows const’s initialization rules.

Predict, change and check

Predict: Will changing var to let print undefined first, or stop with an error?

Test your explanation: Run both declarations separately. Repair the let version by moving the read after initialization, rather than replacing it with var to hide the ordering mistake.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Separate declaration from initialization

Hoisting is a way to discuss how declarations affect a scope before ordinary statements execute; it does not mean the engine physically moves lines in your editor. A var binding exists with undefined before its initializer runs. let and const are also scoped declarations, but accessing them before initialization is a temporal-dead-zone error.

A function declaration is available according to its declaration semantics, while a function expression assigned to a lexical binding still depends on that binding being initialized. typeof is not a universal escape from the temporal dead zone: typeof an uninitialized lexical binding in its own scope can throw. Distinguish an absent global name from a present but uninitialized lexical binding.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.