What is hoisting?
By FrontendPro Editorial Team Updated 9/26/2026
Answer
Hoisting describes the fact that declarations are processed before any code runs. During compilation the engine registers every declaration in its scope, so a name exists from the start of the block even though the line declaring it comes later. Nothing physically moves — what differs is whether the binding is initialised yet.
var is registered and initialised to undefined. let, const and class are registered but left uninitialised: the stretch between the top of the scope and the declaration is the temporal dead zone, and touching the name there throws.
console.log(a); // undefined — declared, not yet assigned
var a = 1;
console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 2;
Function declarations are the exception: both the name and the body are ready before execution, which is why you can call one above its definition. A function expression is only as hoisted as the variable holding it:
greet(); // works
function greet() { return "hi"; }
speak(); // TypeError: speak is not a function
var speak = function () {};
| Declaration | Reading it before the line |
|---|---|
var x | undefined |
let x / const x | ReferenceError |
class X | ReferenceError |
var f = function(){} | undefined (then TypeError if called) |
function f(){} | Works normally |
Why it matters
The TDZ is a feature, not an annoyance — it turns a silent undefined into a loud error at the exact line of the mistake. Declaring variables with const by default, close to where they are used, means hoisting rarely surprises you.
