Hoisting in JavaScript
In the previous article, we discussed the Temporal Dead Zone (TDZ) and why it exists for let and const. To fully understand the TDZ, we need to zoom out and look at the bigger concept behind it: Hoisting.
What is Hoisting?
Hoisting is JavaScript’s way of handling declarations before execution begins.During the compilation phase, the JavaScript engine scans your code and moves all declarations to the top of their scope.
👉 But here’s the key point:
- Declarations are hoisted.
- Initializations are not.
Hoisting with var
what happens behind the scenes
So, var variables exist even before initialization, but they hold the value undefined.
Get vignesh’s stories in your inbox
Join Medium for free to get updates from this writer.
Hoisting with let and const
Both let and const are hoisted too, but they live in the Temporal Dead Zone (TDZ) — the “waiting area” between declaration and initialization.
That’s why trying to access them too early results in an error instead of undefined.
This is the exact behavior we explored in the TDZ article.
Hoisting with Functions
Function Declarations
Function declarations are fully hoisted. You can call them before they appear in code, The function itself is assigned later, so calling it early throws an error.
By understanding Hoisting + TDZ, you can avoid common errors and write cleaner, bug-free JavaScript.
Getting hoisting down is like unlocking a cheat code for understanding how JavaScript runs your code. Once this clicks, a lot of those “weird” JS behaviors start making sense.
👉 Up next, we’ll jump into Scope — the set of rules that decides where your variables and functions can hang out.







