JavaScript provides the feature of using a variable before the declaration. In other words, we can use javascript variables without a declaration.
JavaScript hoisting is the behavior of JavaScript, which moves all the declarations of javaScript variables to the top of the scope.
In other words, JavaScript hoisting moves all the variable declarations to the top of the script.
Let’s consider the following examples:
Example 1
<html> <body> <p id="test"></p> <script> x = 10; // Assign 10 to x element = document.getElementById("test").innerHTML = x; // we first find the element by its id and put value of variable x in a tag. var x; // Declaration of variable x </script> </body> </html>
Output:
10
Example 2
<html> <body> <p id="test"></p> <script> var x; // Declaration of variable x x = 10; // Assign 10 to x element = document.getElementById("test").innerHTML = x; // we first find the element by its id and put value of variable x in a tag. </script> </body> </html>
Output:
10
Explanation:
In example 1 we follow the steps as:
In example 2 we follow the steps as:
Note: Both examples give us the same result. It is because of JavaScript hoisting feature.
JavaScript only hoists the declaration of the variable. It does-not Hoists the initialization of the variable.
Let’s consider the following examples:
Example 3
<html> <body> <p id="test"></p> <script> var m = "Errorsea"; // Initialize variable m var n = "is the best"; // Initialize variable n element = document.getElementById("demo").innerHTML = m + " " + n; // we first find the element by its id and put value of variable m and n in a tag. </script> </body> </html>
Output:
Errorsea is the best
Example 4
<html> <body> <p id="test"></p> <script> var m = "Errorsea"; // Initialize variable m element = document.getElementById("demo").innerHTML = m + " " + n; // we first find the element by its id and put value of variable m and n in a tag. var n = "is the best"; // Initialize variable n </script> </body> </html>
Output:
Errorsea undefined
Explanation:
In example 3, we follow the steps as:
In example 4, we follow the steps as:
Note:
Here both examples don’t give the same results.
In example 4, we use variable n without declaring it. So it uses value undefined because we declare and assign the value at the same time.
Note: We can not use hoisting with variables declared using const and let keyword.
This is all about JavaScript Hoisting. User must be aware of JavaScript hoisting behavior to avoid errors in programming. It is advisable to declare variables at the beginning of the script.
Also Read: JavaScript String Methods
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…