Hoisting in JavaScript

What is Hoisting in JavaScript? How to use Hoisting?

🙋‍♂️ Shubham Verma    🗓 July 28, 2021

Hoisting in JavaScript

What is Hoisting in JavaScript? How to use Hoisting?

Hoisting is one of the default behavior of moving declaration to the top in JavaScript. In this tutorial, we will learn about JavaScript hoisting with the help of examples. The concept of hoisting is very important becase it causes error many time if you haven't use this concept properly.

In JavaScript, Hoisting is a behavior in which a variable or a function can be used before declaration.

In strict mode "use strict;", JavaScript does not allow variables to be used if they are not declared.

For example,

The above codes will work fine and the output will be undefined, internally this will behave as:

If we talk about the variables and constants, keyword var is hoisted and let and const does not allow hoisting.


Hoisting, inside the function:

When we use any variable in the function scope, the variable is hoisted only to the top of the function. Outside the function scope, it will throw an error "Uncaught ReferenceError: X is not defined", For example:

In JavaScript hoisting, the variable declaration is only accessible to the immediate scope. If you are using let then you can not use hoisting behavior. While using let, the variable must be declared first.

Examples with let and const:

Function Hoisting in JavaScript:

A function can be hoisted in JavaScript, It means a function can be called before declaring it. For example,

Here is an issue with function hoisting:

If you are using an function expression it will throw an error, because only declarations are hoisted.

If you use var in the above codes, then you will get another error "Uncaught TypeError: test is not a function".


In this article we implemented Hoisting in JavaScript and learned about What is Hoisting in JavaScript? How to use Hoisting?. We have seen some examples and implemented Hoisting, inside the functions. We read about the Function Hoisting in JavaScript. Now we can implement Hoisting in our javascript projects.

Support our IDKBlogs team

Creating quality content takes time and resources, and we are committed to providing value to our readers. If you find my articles helpful or informative, please consider supporting us financially.

Any amount (10, 20, 50, 100, ....), no matter how small, will help us continue to produce high-quality content.

Thank you for your support!

Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.