Hoisting in JavaScript

Varun Wadhwa
2 min readJun 6, 2021

--

Hoisting means moving variable and function declarations to the top of the scope before code execution takes place. This characteristic of javascript is called Hoisting. It doesn’t matter where we write the variables or function(s), the declaration is moved up to the top of the scope before execution of the code.

Here is how the JavaScript engine sees the code:

Even before the code gets executed, memory allocation has already taken place. All variables are assigned with the value undefined. In the case of a function, the entire code enclosed within the function is displayed in the console.

Let's see what happens when the function is displayed onto console.log() before its declaration.

It can be seen that the entire function gets printed in the console instead of being assigned with an undefined value.

Conclusion

In JS only declarations are hoisted, not the initialization.

PS: If you found this post helpful, please share and leave your comments below.

If you find this post helpful, please consider buying me a coffee! ☕

--

--

Varun Wadhwa
Varun Wadhwa

Written by Varun Wadhwa

I have post graduate in MCA having 6 plus years of experience in web development

Responses (1)