Hoisting

Learn how JavaScript hoists declarations before running your code and why it matters.

Step 1 of 6

JavaScript reads your code twice

Before JavaScript runs a single line of your code, it does a quick scan of the entire scope to find all declarations — variable declarations (`var`, `let`, `const`) and function declarations. It then 'hoists' (lifts) them to the top of their scope conceptually. This means JavaScript knows about your variables and functions before it reaches the line where you wrote them. However, how they behave when accessed early depends on the keyword used. Understanding hoisting explains many confusing JavaScript behaviors.

Think of it this way: Hoisting is like a teacher taking attendance before class starts. JavaScript scans your code first to see who's declared — 'I see a variable called `name`, a function called `greet`...' — then starts executing. But `let` and `const` students can't participate until their actual line is reached — they're marked present but told to wait.
Web Standard
Hoisting is not something JavaScript literally does to your code — it doesn't physically move lines around. It's a mental model for understanding how the JavaScript engine's creation phase works. During this phase, the engine allocates memory for declarations before executing any code.
Learn more on MDN