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 Variables declared with 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.
declarations are moved to the top of their scope during the compilation
phase, before code execution.
Only declarations are hoisted, not initializations.
var are hoisted but initialized as undefined.let and const are hoisted but remain in aHoisting happens per scope (global, function, block) and is handled by
JavaScript’s execution context creation phase.