Hoisting is JavaScript's behaviour of registering declarations in a scope before any code in that scope runs. Function declarations are available everywhere in their scope, var variables exist but hold undefined until their assignment runs, and let, const and class bindings exist but throw a ReferenceError if you touch them before the declaration line (the temporal dead zone).
Nothing is physically moved. During the creation phase of an execution context the engine records every declaration in the scope, then it runs the code from top to bottom. Understanding this explains many surprising results with var, function expressions and closures.
varlet, const and class and the temporal dead zoneBefore a script, module, function or block executes, the engine scans it for declarations and creates the bindings in the matching scope. The assignment (= 5) is not part of that step, it runs later when execution reaches the line.
| Declaration | Scope | Hoisted binding | Value before the declaration line |
|---|---|---|---|
var | Function (or global) | Yes | undefined |
let / const | Block | Yes | Not accessible (ReferenceError) |
class | Block | Yes | Not accessible (ReferenceError) |
| Function declaration | Function or block | Yes | The complete function |
import | Module | Yes | Live binding, usable early |
varA var declaration is hoisted to the top of its function, or to the global scope outside functions, and initialised with undefined.
The engine treats this as if the declaration and the assignment were split:
Because var ignores block boundaries, a variable declared inside an if or for block is visible in the whole function, which is a common source of bugs:
let, const and class and the temporal dead zonelet, const and class declarations are also hoisted, but they are not initialised until execution reaches them. The period between entering the scope and reaching the declaration is the temporal dead zone (TDZ). Reading or writing the binding in that period throws.
The error message proves that b is hoisted. If it were not, the engine would report that b is not defined, or, with an outer b, it would silently read the outer variable. A hoisted TDZ binding shadows the outer one instead:
The TDZ is based on execution order, not on position in the file. A function that reads a const declared later is fine as long as it is called after the declaration has run.
A function declaration is hoisted together with its body, so you can call it before the line where it is written.
A function expression assigned to a variable follows the rules of that variable, which is why it fails when called early:
With let or const the same early call throws a ReferenceError instead of a TypeError. Arrow functions are always expressions, so they behave the same way.
If a var and a function declaration share a name, the function wins at the start, and a later assignment can overwrite it:
In strict mode and in modules, function declarations inside blocks are block scoped. In sloppy mode, browsers apply legacy web-compatibility rules to them, so avoid declaring functions inside if blocks in scripts.
Hoisting is well defined, but code that relies on it is hard to read. These habits remove the surprises:
const by default and let when you must reassign. Avoid var in new code.no-use-before-define, no-var and prefer-const so mistakes show up in the editor.let and const hoisted?Yes. Their bindings are created when the scope is entered, but they stay uninitialised until the declaration runs, so any earlier access throws a ReferenceError.
No. The source is not rewritten. The engine registers declarations first and then executes statements in their original order, so assignments stay where you wrote them.
The variable already exists but holds undefined, and calling undefined raises TypeError: x is not a function. Only function declarations are hoisted with their body.
Yes, but like let they are in the temporal dead zone until the class statement runs. Instantiating a class before its declaration throws a ReferenceError.
Hoisting means declarations are processed before code runs. var is initialised to undefined, function declarations are available in full, and let, const and class exist but cannot be used until their declaration executes. Prefer const and let, declare before use, and let a linter enforce it so your code never depends on hoisting behaviour.