easyJunior Software EngineerSoftware Engineering
What Happens When Variables and Functions Are Hoisted in JavaScript?
Posted 21/01/2026
by Mehedy Hasan Ador
Question Details
What is hoisting in JavaScript and how does it work?
Suggested Solution
Hoisting is JavaScript’s default behavior where variable and function declarations are moved to the top of their scope during the compilation phase, before code execution.
Only declarations are hoisted, not initializations.
- Function declarations are fully hoisted and can be used before they are defined.
- Variables declared with
varare hoisted but initialized asundefined. - Variables declared with
letandconstare hoisted but remain in a Temporal Dead Zone until their declaration is reached.
Hoisting happens per scope (global, function, block) and is handled by JavaScript’s execution context creation phase.