Programming languages / Hoisting in JavaScript

Hoisting in JavaScript

Why JavaScript lets you call a function above its definition, why var gives undefined instead of an error, and what the temporal dead zone of let and const protects you from.

Beginner7 steps~6 min
Ready slot — a name in memory that can be used
Undefined slot — a var before its assignment runs
Dead-zone slot — let or const before its line runs
// THE SHORT VERSION

In 30 seconds

Before running a script, JavaScript scans it and reserves every declared name. This is hoisting: declarations take effect from the top of their scope, wherever you wrote them.

A function declaration is hoisted whole, so you can call it above its definition. A var is hoisted as undefined: using it early gives undefined, not an error.

let and const are reserved too, but reading them before their line throws a ReferenceError. That stretch is the temporal dead zone.

Hoisting in JavaScript, step by step

STEPS01 / 07

Shown for a single scope; the same happens inside every function and block. Error messages are those of Chrome and Node.js; other browsers word them differently.

STEP 01 / 07 Called before it is defined
THE MATH
greet(); // line 1 function greet() // line 2 output: hi
  1. Step 1: Called before it is defined. This program calls greet on line 1, but the function is not written until lines 2 to 4. Reading from the top, line 1 uses a name that nobody has defined yet, so you might expect an error. Instead it runs and prints hi. The reason is a behaviour called hoisting.
  2. Step 2: Two passes over the code. JavaScript does not simply run a script from the top. Before executing anything, the engine goes through the whole scope, finds every declaration and reserves each name in memory. Only then does it run the lines in order. Hoisting is the nickname for that first pass: nothing moves, but declarations act as if lifted to the top.
  3. Step 3: Function declarations. A function declaration, a statement that starts with the word function, is hoisted completely: pass 1 stores the name and the whole function body. So when pass 2 reaches line 1, greet is already in memory, ready to be called. That is why you can put helper functions at the bottom of a file and call them at the top.
  4. Step 4: var: hoisted as undefined. A var declaration is hoisted too, but only the name. Pass 1 reserves score and fills it with the placeholder undefined; the assignment = 7 stays on line 2 where you wrote it. So line 1 is not an error. It quietly prints undefined, which is rarely what you meant and easy to miss.
  5. Step 5: let and const: the dead zone. let and const are hoisted as well: the name is reserved in pass 1. But it gets no placeholder value, and touching it before its declaration line has run is an error. That stretch, from the start of the scope to the declaration, is the temporal dead zone. It turns a silent undefined into a loud, early error.
  6. Step 6: Function expressions. Only declarations are hoisted, never assignments. Here the function is a value assigned to a var, so pass 1 reserves run as undefined and nothing more. Calling undefined on line 1 fails. Written with let or const instead, the same line would fail with the dead-zone ReferenceError. Arrow functions behave the same way.
  7. Step 7: The four cases, side by side. Everything declared is hoisted; what differs is the state it is left in. A function declaration is complete and callable. A var holds undefined. A let or const sits in the dead zone until its line runs. The habit that sidesteps all of it: declare before you use, and prefer let and const so that slips fail loudly.
VALUES SO FAR
outputhiconsole
pass 1····set up
pass 2····run
function····hoisted
var····hoisted
let····error
const····error
run()····var
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

// AFTER

Learn next

Nothing builds on this one yet. See every programming languages concept.

Spotted a mistake on this page?