JavaScript Hoisting
Hoisting describes how JavaScript handles declarations before running your code. The engine first scans a scope for declarations and sets them up, and only then executes the statements line by line. The effect is as if some declarations were "moved to the top" — but different kinds of declarations are hoisted in different ways.
| Declaration | Hoisted? | Usable before its line? |
|---|---|---|
function name() {} |
✅ fully (name + body) | ✅ yes |
var x = 1 |
✅ name only, initialised to undefined |
⚠️ yes, but it's undefined |
let / const |
✅ name only, not initialised | ❌ ReferenceError (temporal dead zone) |
class Name {} |
like let |
❌ ReferenceError |
const f = () => {} |
like const |
❌ ReferenceError |
Function declarations
Function declarations are hoisted completely, so you can call them before they appear in the code. This lets you put the main logic at the top and helper functions below:
console.log(area(3, 4));
printReport();
function area(w, h) {
return w * h;
}
function printReport() {
console.log("Report printed");
}12
Report printed
var: hoisted as undefined
A var declaration is hoisted, but its assignment stays where it is. Before that line, the variable exists with the value undefined:
console.log(score); // undefined, not an error
var score = 10;
console.log(score);undefined
10
The engine effectively treats this as:
var score; // declaration hoisted
console.log(score);
score = 10; // assignment stays in place
console.log(score);Silently getting undefined hides bugs, which is one reason var has fallen out of favour.
let and const: the temporal dead zone
let, const and class are hoisted too — the engine knows about them from the start of their block — but they stay uninitialised until their declaration line runs. Accessing them earlier throws a ReferenceError. The period between the start of the block and the declaration is the temporal dead zone (TDZ):
try {
console.log(total);
} catch (err) {
console.log(err.message);
}
let total = 50;
console.log(total);
const shadowTest = "outer";
{
try {
console.log(shadowTest); // the inner one below is already in scope!
} catch (err) {
console.log(err.message);
}
const shadowTest = "inner";
}Cannot access 'total' before initialization
50
Cannot access 'shadowTest' before initialization
The second example proves hoisting happens: if the inner shadowTest weren't hoisted, the line would print "outer". Instead, the inner variable already "owns" the name for the whole block.
Function expressions and arrow functions
A function stored in a variable follows the rules of that variable, not of function declarations:
try {
greet();
} catch (err) {
console.log(err.name + ": " + err.message);
}
const greet = () => console.log("hi");
greet();
try {
legacy();
} catch (err) {
console.log(err.name + ": " + err.message);
}
var legacy = function () {};ReferenceError: Cannot access 'greet' before initialization
hi
TypeError: legacy is not a function
With var, the variable exists but holds undefined — so calling it fails with a TypeError instead.
Best practices
- Declare variables at the top of their scope (or right before first use) — don't rely on hoisting.
- Use
constandlet; the TDZ turns mistakes into clear errors. - Calling function declarations before their definition is fine and common.
- In classes, define everything before you create instances — classes aren't usable before their declaration.
Note: Hoisting isn't literally moving code. It's a consequence of how the engine creates a scope: all declarations are registered when the scope is entered, before any statement runs.
Test yourself
What does console.log(x); var x = 5; print?
- 5
- undefined
- ReferenceError
- null
What happens with console.log(y); let y = 5;?
- Prints undefined
- Prints 5
- ReferenceError
- Prints null
Which can be called before its line in the code?
- const f = function () {}
- const f = () => {}
- function f() {}
- class F {}