What Is Hoisting in JavaScript?
Hoisting is the most misunderstood JS concept. Here is what actually happens and why variables behave differently.
What Is Hoisting in JavaScript?
Hoisting is the behavior where JavaScript makes variables and functions available before their declaration line in the code. It is one of the most famous interview topics.
What Actually Happens
Hoisting is not the engine physically moving code. It is a side effect of the memory allocation phase of execution context creation.
When the engine enters a new execution context, it scans the code and allocates memory for all declarations before running any line:
varvariables: memory allocated, value set toundefined.- Function declarations: memory allocated, entire function body stored.
letandconst: memory allocated, but placed in the Temporal Dead Zone (uninitialized).
Variables Hoisted With undefined
console.log(x); // undefined var x = 5; console.log(x); // 5
x is not a ReferenceError because memory was reserved. But the assignment x = 5 only runs during the code execution phase. So before that line, x is undefined.
Function Declarations Hoisted With Body
foo(); // "hello" function foo() { console.log("hello"); }
The whole function is stored in memory, so calling it before the declaration works.
Function Expressions Are Not Hoisted the Same Way
bar(); // TypeError: bar is not a function var bar = function () { console.log("hello"); };
bar is a var variable, hoisted with undefined. Calling undefined() throws a TypeError.
let and const Are Hoisted Differently
They are hoisted (memory is allocated) but not initialized. Accessing them before the declaration line throws a ReferenceError because they are in the Temporal Dead Zone.
console.log(y); // ReferenceError let y = 5;
The Takeaway
Hoisting is a side effect of the memory allocation phase. var variables are hoisted with undefined, function declarations with their full body, and let/const are hoisted but kept uninitialized in the TDZ. Understanding this prevents bugs and answers interview questions.
Hoisting is the behavior where variables and functions are available before their declaration line. It is a side effect of the memory allocation phase of execution context creation, not actual code movement.
No. var variables are hoisted with undefined. Only the declaration is hoisted; the assignment happens during the code execution phase when the line is reached.
Yes. Function declarations are hoisted with their entire body. You can call a function before its declaration line in the source code.
Memory is allocated for them, so technically yes. But they are placed in the Temporal Dead Zone and remain uninitialized until the declaration line. Accessing them early throws a ReferenceError.
Because the variable is hoisted with undefined (with var). Calling undefined() throws TypeError: x is not a function. Only function declarations are hoisted with their body.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

