JavaScript Declarations are Hoisted
In JavaScript, a variable can be declared after it has been used.
In other words; a variable can be used before it has been declared.
Example 1 gives the same result as Example 2:
Example(1)
<script> x = 5; // Assign 5 to x elem = document.getElementById("demo1"); // Find an element elem.innerHTML = x; // Display x in the element var x; // Declare x </script>
Example(2)
<script> var x; // Declare x x = 5; // Assign 5 to x elem = document.getElementById("demo"); // Find an element elem.innerHTML = x; // Display x in the element </script>
To understand this, you have to understand the term "hoisting".
Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).
The let and const Keywords
Variables defined with let
and const
are hoisted to the top of the block, but not initialized.
Meaning: The block of code is aware of the variable, but it cannot be used until it has been declared.
Using a let
or const
variable before it is declared will result in a ReferenceError
.
The variable is in a "temporal dead zone" from the start of the block until it is declared:
Example(3)
This will result in a ReferenceError
:
<script> // Check the console, this next line will produce an error: carName = "Volvo"; document.getElementById("demo").innerHTML = carName; let carName; </script>
JavaScript Initializations are Not Hoisted
JavaScript only hoists declarations, not initializations.
Example 4 does not give the same result as Example 5:
Example(4)<script> var x = 5; // Initialize x var y = 7; // Initialize y elem = document.getElementById("demo"); // Find an element elem.innerHTML = x + " " + y; // Display x and y </script>
<script> var x = 5; // Initialize x elem = document.getElementById("demo"); // Find an element elem.innerHTML = "x is " + x + " and y is " + y; // Display x and y var y = 7; // Initialize y </script>
Does it make sense that y is undefined in the last example?
This is because only the declaration (var y), not the initialization (=7) is hoisted to the top.
Because of hoisting, y has been declared before it is used, but because initializations are not hoisted, the value of y is undefined.
Example 5 is the same as writing:
Example(6)
<script> var x = 5; // Initialize x var y; // Declare y elem = document.getElementById("demo"); // Find an element elem.innerHTML = x + " " + y; // Display x and y y = 7; // Assign 7 to y </script>
Declare Your Variables At the Top !
Hoisting is (to many developers) an unknown or overlooked behavior of JavaScript.
If a developer doesn't understand hoisting, programs may contain bugs (errors).
To avoid bugs, always declare all variables at the beginning of every scope.
Since this is how JavaScript interprets the code, it is always a good rule.
<!DOCTYPE html> <html> <head> <title>How Can I Work With Hoist In JavaScript With Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> </head> <style> body{ background: black; } </style> <body> <div class="container"> <br> <div class="text-center"> <h1 id="color" style="color: White">How Can I Work With Hoist In JavaScript</h1> </div> <br> <br> <div class="well"> <h2 id="demo1"></h2> <h2 id="demo2"></h2> <h2 id="demo3"></h2> <h2 id="demo4"></h2> <h2 id="demo5"></h2> <script> var x; // Declare x x = 5; // Assign 5 to x elem = document.getElementById("demo1"); // Find an element elem.innerHTML = x; // Display x in the element var x; // Declare x </script> </div> </div> </body> </html>