DEV Community

Cover image for ๐Ÿ”ฅ๐Ÿ•บ๐Ÿผ JavaScript Visualized: Hoisting
Lydia Hallie
Lydia Hallie

Posted on • Edited on

๐Ÿ”ฅ๐Ÿ•บ๐Ÿผ JavaScript Visualized: Hoisting

Hoisting is one of those terms that every JS dev has heard of because you googled your annoying error and ended up on StackOverflow, where this person told you that this error was caused because of hoisting ๐Ÿ™ƒ So, what is hoisting? (FYI - scope will be covered in another post, I like to keep posts small and focused)

If youโ€™re new to JavaScript, you may have experienced โ€œweirdโ€ behavior where some variables are randomly undefined, ReferenceErrors get thrown, and so on. Hoisting is often explained as putting variables and functions to the top of the file but nah, thatโ€™s not whatโ€™s happening, although the behavior might seem like it ๐Ÿ˜ƒ

When the JS engine gets our script, the first thing it does is setting up memory for the data in our code. No code is executed at this point, itโ€™s simply just preparing everything for execution. The way that function declarations and variables are stored is different. Functions are stored with a reference to the entire function.

With variables, itโ€™s a bit different. ES6 introduced two new keywords to declare variables: let and const. Variables declared with the let or const keyword are stored uninitialized.

Variables declared with the var keyword are stored with the default value of undefined.

Now that the creation phase is done, we can actually execute the code. Let's see what happens if we had 3 console.log statements on top of the file, before we declared the function or any of the variables.

Since functions are stored with a reference to the entire function code, we can invoke them even before the line on which we created them! ๐Ÿ”ฅ

When we reference a variable declared with the var keyword before their declaration, itโ€™ll simply return its default value that it was stored with: undefined! However, this could sometimes lead to "unexpected" behavior. In most cases this means youโ€™re referencing it unintentionally (you probably donโ€™t want it to actually have the value of undefined) ๐Ÿ˜ฌ

In order to prevent being able to accidentally reference an undefined variable, like we could with the var keyword, a ReferenceError gets thrown whenever we try to access uninitialized variables. The "zone" before their actual declaration, is called the temporal dead zone: you cannot reference the variables (this includes ES6 classes as well!) before their initialization.

When the engine passes the line on which we actually declared the variables, the values in memory are overwritten with the values we actually declared them with.

(Oops I notice now this should be number 7. Will update asap ๐Ÿ˜ฌ)


All done! ๐ŸŽ‰ Quick recap:

  • Functions and variables are stored in memory for an execution context before we execute our code. This is called hoisting.
  • Functions are stored with a reference to the entire functions, variables with the var keyword with the value of undefined, and variables with the let and const keyword are stored uninitialized.

I hope that the term hoisting is a bit less vague now that we've looked at what's happening when we execute our code. As always, don't worry if it still doesn't make a lot of sense yet. You'll get a lot more comfortable with it the more you work with it. Feel free to ask me for help, I'd love to help you! ๐Ÿ˜ƒ

Latest comments (62)

Collapse
 
faisalmh4045 profile image
Faisal Mahmud • Edited

@lydiahallie This series is really helpful and easy to follow. It explains hoisting in a way that actually makes sense.
I also wanted to mention that the images do not seem to be accessible anymore. They added a lot to the explanation.
Thanks for sharing this!

Collapse
 
tiagosv profile image
Tiago

Images are broken ๐Ÿ˜ž

Collapse
 
picwellwisher12pk profile image
Amir Hameed

I believe I had taken one of your courses on Frontend Masters

Collapse
 
kirankamath96 profile image
Kiran Kamath • Edited

@lydiahallie can you add the gifs again, I guess they are not visible now.

Collapse
 
craizytech profile image
Eammon Kiprotich

Could we have a fix on the post please, I am seeing image is nolonger available

Collapse
 
the_practitioner profile image
Gibran Castillo

Thank you for the detailed and well-illustrated explanation.

Collapse
 
gizelads profile image
Gizela Delgado Soto

Great article!

Collapse
 
sagaryal profile image
Sagar Aryal

Among many superficial JavaScript tutorials and articles, this one really stands out with underlying understanding.

Collapse
 
itsjatin_kumar profile image
Jatin Kumar

crisp and to the point...๐Ÿ”ฅ

Collapse
 
sieuthuattoan profile image
Le Quang Hung

thanks for the sharing, this is very clear.
i have an additional question. pls help
what if i just use a variable without declare them anywhere.
like this:

x = 10;
console.log(x);

is the default is "var"?