Hoisting means JavaScript sets up a scope's declarations before it runs any code in that scope. The code then behaves as if the declarations moved to the top, although nothing moves in the source.
Variables declared with var are hoisted and initialized with undefined. Variables declared with let and const are hoisted but not initialized, so reading one before its declaration throws a ReferenceError. That stretch is called the temporal dead zone. Function declarations are hoisted whole, so you can call them before the line that defines them.
console.log(x); // undefined (not error)
var x = 5;
console.log(y); // ReferenceError
let y = 10;
Rewriting in plainer words…
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Saved in this browser - sign in to keep your review list.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Keep going - a few more words and AI can grade it.
Interview lens
How interviewers actually use this question
Likely follow-ups
Why does calling a function expression stored in var before its line throw, while a function declaration works?Only the var name hoists, set to undefined, so the call throws a TypeError. A declaration hoists the whole function.
If let is hoisted, how could you show it? Why not just say it isn't hoisted?Use shadowing. An inner-block let x hides an outer x from the top of the block, so reading x early throws instead of reading the outer one.
A function reads a let variable declared below it. When does that work and when does it throw?The TDZ is about time, not position. The call works if it runs after the declaration line has executed, and throws if it runs before.
Are class declarations hoisted like function declarations?They hoist like let. The name sits in the TDZ, so using the class before its declaration throws a ReferenceError.
Hoisting means JavaScript sets up every declaration in a scope before it runs any code in that scope, so they act as if they sat at the top.
Nothing moves in the source. The engine creates the bindings first, then runs the code line by line.
A var exists from the start and holds undefined, so reading it early gives you undefined, not an error.
let and const are hoisted too, but they aren't initialized, so reading one before its declaration throws a ReferenceError. That gap is the temporal dead zone.
Function declarations hoist whole, so I can call one above the line that defines it.
In practice I use let and const, because the TDZ turns a silent undefined bug into an error you see right away.
Weak answers to avoid
Says the code is physically moved to the topNothing moves. The engine creates the bindings before it runs the scope. Say that, then explain why the code behaves as if the declarations were at the top.
Claims let and const are not hoistedThey are hoisted but left uninitialized. The early ReferenceError comes from the temporal dead zone, not from a missing variable.
Says the assigned value is hoisted tooOnly the declaration hoists. Reading a var before var x = 5 gives undefined, not 5. The assignment still runs on its own line.
Treats function expressions like declarationsOnly function declarations hoist whole. A function expression stored in a var is undefined before its line, so calling it early throws a TypeError.
Interview lens
Likely follow-ups, what you can say, and the weak answers to avoid.