LearnThatStack Ace your next interview
JavaScript · question
Question 2 of 126

What is hoisting in JavaScript?

beginner
← All JavaScript questions
Re-explain

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.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for JavaScript One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

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.
  • Beyond hoisting, how do var, let and const differ? Cover function scope vs block scope, redeclaration, reassignment, and that const locks the binding, not the contents of an object.

What you can say

  1. 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.
  2. Nothing moves in the source. The engine creates the bindings first, then runs the code line by line.
  3. A var exists from the start and holds undefined, so reading it early gives you undefined, not an error.
  4. 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.
  5. Function declarations hoist whole, so I can call one above the line that defines it.
  6. 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 top Nothing 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 hoisted They 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 too Only 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 declarations Only 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.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the JavaScript cheatsheet.

← Back to all JavaScript questions
Pro · $10/mo

110 of 126 JavaScript answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime