
Hoisting
Understanding Hoisting and Execution Context in JavaScript
Let's dive into a fundamental yet often confusing concept in JavaScript—Hoisting. By the end of this blog, you'll have a clearer understanding of memory allocation, undefined, and why JavaScript behaves the way it does.
Let's Observe This Code:
getName(); // Hello
console.log(x); // undefined
var x = 7;
function getName() {
console.log("Hello");
}Why Doesn’t This Throw an Error?
- In many other programming languages, this code would throw an outright error. Why? Because accessing a variable or function that hasn’t been defined yet is not allowed.
- But in JavaScript, something magical happens during the Memory Creation Phase of the Execution Context.
The Memory Creation Phase
- During this phase:
- Variables are assigned a placeholder value of
undefined. - Function declarations are stored in memory with their entire code.
- Variables are assigned a placeholder value of
- This means that when we execute the code line by line, it can access the
undefinedvalue of variables or the complete function in memory.
So, in the example above:
getName()works because the function was stored in memory during the creation phase.console.log(x)outputsundefinedbecause the variablexwas also allocated memory but wasn’t assigned a value yet.
What Happens If We Remove var x = 7;?
If you remove the line var x = 7;, the code will throw an error: Uncaught ReferenceError: x is not defined
Why? Because now the variable x is not declared at all, so JavaScript doesn’t allocate memory for it.
What is Hoisting?
Hoisting is a concept in JavaScript that lets us access variables and functions before we initialize or define them. This happens because of the Memory Creation Phase.
Think of it like this:
JavaScript scans through the code before executing it and creates memory space for variables and functions.
Key takeaways about hoisting:
- Variables declared with
varare hoisted but initialized asundefined. - Functions are hoisted with their full definitions, so you can call them before they appear in the code.
Here's Another Example:
getName(); // Hello
console.log(x); // undefined
console.log(getName); // Returns the whole function definition
function getName() {
console.log("Hello");
}
Explanation:
- The function
getName()was already stored in memory, so calling it works perfectly fine. - The variable
xwas also declared but not yet assigned a value, so it logsundefined. - When you log
getName, you get the entire function definition because it was fully hoisted.
Arrow Functions and Hoisting
Now, let’s observe a different example with arrow functions:
getName2();
// Uncaught TypeError: getName2 is not a function
getName3();
// Uncaught TypeError: getName3 is not a function
console.log(getName);
var getName2 = function () => {
//
}
var getName3 = () => {
//
}What’s Happening Here?
- In the case of arrow functions, the function is stored as a value inside a variable.
- During the memory allocation phase, variables like
getName2andgetName3are assignedundefined. - When you try to execute
getName2()orgetName3(), JavaScript seesundefined()and throws aTypeErrorbecauseundefinedis not a function.
Key Takeaways :-
- Variable Declarations: Variables declared with
varare hoisted but initialized asundefined. - Function Declarations: Functions are fully hoisted and ready to use, even before their definition appears in the code.
- Arrow Functions: When assigned to a
var, they behave like variables and are hoisted asundefined. This means you can’t call them before their definition.
By understanding how JavaScript allocates memory and executes code, you’ll gain deeper insights into debugging and writing error-free code. Remember: Hoisting is your friend—but only if you know how it works.
Happy coding! 🚀

Browse More Blogs

A Developer's Guide to Building AI Agents with the Model Context Protocol (MCP)
Go beyond chatbots. Learn how to use the Model Context Protocol (MCP) to build powerful, real-world AI agents that can automate workflows, assist in coding, man

Null & Undefined
Understand null vs undefined in JavaScript. When to use each, common mistakes, and best practices.

A Freelance Project for an Enterprise
Enterprise freelance project case study: business web development, client collaboration, and delivery.
Let's Create Something Amazing Together!
Whether you have a project in mind or just want to connect, I'm always excited to collaborate and bring ideas to life.
Continue the Journey
Thanks for taking the time to explore my work! Let's connect and create something amazing together.