What Is Hoisting in JavaScript?
Hoisting is JavaScript's behavior of processing variable and function declarations before executing code, making them "available" earlier in scope than where...
Published November 20, 2026
Hoisting is JavaScript's behaviour of processing declarations before running code, so functions and var variables can be used before the line that declares them. let and const are hoisted too, but cannot be accessed until their declaration (the temporal dead zone).
The concept
Function declarations are fully hoisted: you can call them before they appear. var is hoisted and initialised to undefined; let/const are hoisted but uninitialised.
Function expressions and arrow functions assigned to variables are only as hoisted as the variable they are stored in.
What really happens
Practical examples
var
console.log(a) // undefined
var a = 5let/const (TDZ)
console.log(b) // ReferenceError
let b = 5Functions
hello() // ok
function hello() { console.log('hi') }
say() // TypeError: say is not a function
var say = () => console.log('hi')Common mistakes
- Relying on hoisting of var.
- Calling function expressions before they are assigned.
- Using var in loops.
Key takeaways
- Declarations are processed first.
- var → undefined, let/const → TDZ.
- Declare before use.
FAQ
Is code really moved?
No, it is a mental model of how scope is set up before execution.
Does hoisting apply to classes?
Yes, but they stay in the TDZ until declared.