Variable Hoisting in Javascript
Posted By : Manish Nayal | 28-Jun-2021
Introduction
Variable hoisting is one of the major feature of javascript, it allows a developer to understand Javascript's way of handling the variable's declaration. By bringing up all the variables’ declaration at the top of the functional/global block is called variable hoisting. This allows the user to use the variable before declaring it. There are some variables that get hoisted completely and there are some that get hoisted with a temporal dead zone.
Hoisting of a Variable
Consider the a forementioned example and guess the output:
console.log("not declared but using variable age --> ", age)
var age = 10;
As we are using a variable age before its declaration, it will throw the error ‘age is not defined’ through most of the programming language. But in JavaScript, it does not. Then the output would be 10 or something !!! But….The output would be: undefined
Hence, why undefined and not 10? Javascript only pulls up the declarations and not their initialization or assignments. JavaScript looks at the same code differently:
var age;
console.log("not declared but using variable age -->", age)
age = 10;
Only JavaScript can hoist the declaration!
Function Hoisting
JavaScript hoist functions too. We can access the functions before writing them.But there are two different cases in a function as there are two ways to create a function:-
1) Using function expression
2) Creating variables for a function
We will understand both and how each function behaves towards variable hoisting.
Using Function Declaration:
We are calling the helloWorld() function before declaring it. This can be done since JavaScript hosts the function definition at the top of the scope.
helloWorld();
function helloWorld(){
alert('hello world!!!');
};
Below is how JavaScript reads the above code:
function helloWorld(){
alert('hello world!!!');
};
helloWorld();
Creating Variables for a Function
Let’s understand how hoisting works and create a function using variables:
helloWorld();
var helloWorld = function() {
alert('hello world!!!');
}
If you execute the above code, you will be thrown an Uncaught Exception error as shown in the screenshot below:
at <anonymous>:1:1
Does that mean Javascript doesn't hoist the function declared using var. To answer this, we have to repeat the definition of Variable Hoisting again, bringing up all variable declaration at the top of the functional/global block is called variable hoisting. That means, JavaScript allows the user to hoists the function declared using a function expression syntax for the definition, and that’s how it is available to invoke before the execution reaches there. This is everything about variable hosting. ES6 has introduced new variable types called let and const. These variable gets hoisted but JavaScript puts them into Temporal Dead Zone and makes them unavailable for usage until the execution reaches there. The reason for this behavior is that JavaScript wants to minimize runtime errors.If you try to use any let or const variable before declaring, you will land up with an error that reads<variable_name> is not defined.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Manish Nayal
He likes making stuff on the Internet and specialises in designing digital products such as websites and web apps. He is frontend developer having good knowledge of various frontend technologies.