Skip to main content

Command Palette

Search for a command to run...

Hoisting in JavaScript explained visually πŸ”₯ 😍

Published
β€’5 min readβ€’View as Markdown
Hoisting in JavaScript explained visually πŸ”₯ 😍
N

const Bio = ["Web Developer", "Freelancer" ,"UI/UX Designer" ,"Technical Writer"]

Developers often have a love-hate relationship with JavaScript because sometimes it doesn't work as expected. Learning JavaScript fundamentals and it's quirks can assist developers in debugging and writing clean code. This blog post aims to explain one such peculiarity, i.e. hoisting using animated gifs 🌟✨. After reading this blog, you will be one step closer to becoming a Rockstar Developer 🎸😎.

getStarted.avif

Execution of JavaScript code.

Before understanding the concept of hoisting let’s first learn how javascript will run code behind the scene through an example.

var a = 2;
var b = 4;

var sum = a + b;

console.log(sum);

In this simple example, we initialize two variables, a and b, and store 2 and 4, respectively. Then we add the value of a and b and store it in the sum variable. Let's see how JavaScript will execute the code in the browser πŸ€–.

When the JS first receives our code, it will create an execution context. Execution context will first scan through all the code, and allocate memory to all the variables and functions. For variables specifically var, JavaScript will store undefined in the memory, and for functions, it will keep the entire function code reference in memory.

execContext1.1.gif

Now that we have scanned and allocated memory to the code, we can execute the code. JavaScript will again start going through the whole code line by line.

As it encounters var a = 2, it assigns 2 to 'a' in memory. Until now, the value of 'a' was undefined.

Similarly, it does the same thing for the b variable. It assigns 4 to 'b'. Then it calculates and stores the value of the sum in memory which is 6. Now, in the last step, it prints the sum value in the console and then destroys the global execution context as our code is finished.

execContext1.2.gif

If you want to learn more about execution of code in JavaScript, I wrote an article about it on the dev community.

Link: https://dev.to/narottam04/how-javascript-works-visually-explained-269j

Hoisting in var

To understand hoisting in a variable defined with var, let's take the example we took in the execution of code. However, in this example, we're going to print the sum at the beginning, not at the end.

console.log(sum);

var a = 2;
var b = 4;

var sum = a + b;

The above code would throw an error in other programming languages, but not in JavaScript.

code2.png

As we saw in the section on the execution of code, when JS first receives our code, it will create an execution context. Execution context will first scan through all the code, and allocate memory to all the variables and functions.

execContext1.1.gif

Now after scanning the code it will start the execution of the code line by line. In the first line, javascript checks the value of sum in memory and prints undefined as an output.

Now, as it encounters var a = 2, it assigns 2 to 'a' in memory. Until now, the value of 'a' was undefined.

Similarly, it does the same thing for the b variable. It assigns 4 to 'b'. Then it calculates and stores the value of the sum in memory which is 6.

hoisting2.2.gif

If we console log after we calculate the sum let’s see the output.

code-2.4.png

Initially, it will print undefined, after computing the sum, it will print 6. This peculiarity in javaScript is called hoisting. Let’s learn hoisting in let, const keywords and functions.

Hoisting in let and const

When we refer to a variable declared with the var keyword before its declaration, it simply returns its default value: undefined! However, as demonstrated in the previous example, this can sometimes result in "unexpected" behavior.

Therefore, ES6 introduced two new JavaScript keywords to combat this unexpected behavior: let and const. These two keywords are Block Scope. To keep this article concise we would be learning more about the scope in another blog.

Let’s see the hoisting in let and const through an example.

console.log(a);
console.log(sum);

var a = 2;
let b = 4;
const c = 6;

const sum = a + b + c;

So let’s start with the execution of code again, when JS first receives our code, it will create an execution context. Execution context will first scan through all the code, and allocate memory to all the variables and functions. I know I've mentioned this multiple times, but it's important πŸ˜….

As opposed to var where undefined is stored in memory, let and const variables are uninitialized.

hoisting1.1.gif

After scanning the code it will start executing the code line by line. The first line will print the value of β€˜a' i.e. undefined in the console. But when we try to access a let and const keyword before initialization, a ReferenceError gets thrown whenever we try to access uninitialized variables. Uninitialized variables cannot be accessed since they are in a temporal dead zone.

Temporal dead zone simply means we don't have access to the variables before declaration.

code-4.png

If we try to print the value of β€˜b’ instead of the sum the result would be the same.

code4.1.png

The value of the sum must be printed once the deceleration of the value to the variable is complete.

code4.2.png

Here is the visualization of the execution of code.

hoisting3.gif

Hoisting in function

When javaScript begins scanning code and comes across a function, it stores the function's reference in memory. It is the reason why we can invoke the function before we have created it. Let’s see it in action through an example.

printSum(4,5)

function printSum(a,b){
    const c = a + b;
    console.log(c);
}

The output of the following code will be.

code5.png

Let's try to visualize, first JavaScript will scan the code, it will encounter a function in the 3rd line, and it will add a reference of the function to the memory.

hoisting4.gif

Javascript will now run the code line by line. It comes across a function on the first line. When a function is executed in javascript, it creates a new execution context within our global execution context. Consider the function execution context to be a mini execution context, with its memory and a location to execute the code in the function.

Read more about how functions execute code in this blog post.

hoisting5.gif

Once all the code inside the function is executed, the execution context of the function is destroyed.

Reference

https://media.giphy.com/media/xUPOqo6E1XvWXwlCyQ/giphy.gif

I hope this post was informative. πŸ’ͺ🏾 Feel free to comment or reach out to me if you have any questions.

Comments (10)

Join the discussion
P

Great one

1
N

Glad you liked it! Paul Oloyede

P

Very useful content, thanks for sharing πŸ™ŒπŸ»

3
R

Thank you so much for this blog post. It was very helpful and informative in learning about the hoisting feature of javascript.

1
N

Glad you liked it Ranvir Choudharyβ™₯

J

I really like this article! You explained things clearly.

I finally understand how var and let are different

1
R

Excellent... Simplicity... Knowledge...πŸ‘ŒπŸ‘ŒπŸ‘Œ

1
M

Thank you for sharing this article.πŸ™

1
K

Wonderful article, Narottam. I enjoyed reading the whole blog from start to end.

Writing, sentence formation and content are precise and well formatted - making it easy to read and interpret. GIFs are acting as a cherry on top.

How do you make those GIFs? I am curious.

6
N

Thank you Kaushal Joshi for such kind words β™₯. I am using powerpoint 2019 for creating gifs. While I wanted to use Canva, Powerpoint gives me more control over custom animation.

4
K

Wow! impressive :)

1
A

Narottam Sahu It was very clean and very readable. I'm a huge visual person so this is was very helpful. I initially thought hoisting meant that the interpreter bounced by to the top. But your tutorial shows that it doesn't it just saves stuff in memory.

2
N

Thank you Alex Wilkins!

M

Amazing content βœŒοΈπŸ‘

1
S

Found this blog very useful and interesting. Got to learn a lot of new things also the language used was really easy to understand. Would love to see such informative vlogs in the future.

2
V

Great content

1
N

Thank you Vedant Sheth

2