Hosting in JavaScript: Day 9

Introduction

 
I highly recommend reading the following previous parts:
  1. Introduction to JavaScript: Day 1
  2. Programming Basics in JavaScript: Day 2
  3. Comments and Objects in JavaScript: Day 3
  4. Scope and Events in JavaScript: Day 4
  5. Array in JavaScript: Day 5
  6. Page Re-direction in JavaScript: Day 6
  7. Errors and Exception Handling in JavaScript: Day 7
  8. Debugging in JavaScript: Day 8

Hoisting in JavaScript

 
It is the default behavior of JavaScript. Hoisting means moving the declarations to the top of the function. It likes you declare all variables top in a function. Rather than being available after their declaration, they might actually be available beforehand. 
 
Example
  1. <script>    
  2.    var hoist = "Outer";    
  3.    function Hoisting(){    
  4.       alert(hoist);     
  5.       var hoist = "Inner";    
  6.       alert(hoist);     
  7.    }    
  8.    Hoisting();    
  9. </script>   
In the preceding example, you are guessing the output like the first popup message is "Outer" because it is not yet changed. It moves the declaration to above the function and over writes, that's why the first alert message is "undefined" because the default behavior of JavaScript means Hoisting.
 
It is the same as in the following example:
  1. <script>    
  2.    var hoist = "Outer";    
  3.    function Hoisting(){    
  4.       var hoist;    
  5.       alert(hoist);     
  6.       hoist = "Inner";    
  7.       alert(hoist);    
  8.    }    
  9.    Hoisting();    
  10. </script>   
To avoid the bugs, always declare variables at the top. JavaScript only hoists the declaration of variables but not the initializations.
 
Example
  1. <script>    
  2.    function Hoisting()    
  3.    {    
  4.       var hoist = "JavaScript";    
  5.       alert(hoist + " " + example);    
  6.       var example = "Hoisting Example";    
  7.    }    
  8.    Hoisting();    
  9. </script>   
Output
 
JavaScript is only hoisting the declaration, not the initialization. It shows in the preceding example the output is “JavaScript undefined”.
 
This JavaScript hoisting is not only for variables but also for functions. It is the same for variable declarations as for functions. This only hosts the function declaration, not the function expressions.
 
Example
  1. <script>    
  2.    Hoisted();    
  3.    NotHoisted();    
  4.    function Hoisted() {    
  5.       alert("Function hoisted!");    
  6.    }    
  7.    var NotHoisted = function () {    
  8.       alert("Function not hoisted!");    
  9.    };    
  10. </script>  
Output
 
The preceding example output first alert message is "Function hoisted!" and the second alert message does not show because the error "undefined is not a function" variable declaration is hoisted.
 
If you used the named function expression:
  1. <script>    
  2.    fName();    
  3.    vName();    
  4.    var vName = function fName() {    
  5.       console.log("Definition not hoisted!");    
  6.    };    
  7. </script>   
Output
 
You can see in the JavaScript console log that it will show the error "fName is not defined" because the function name isn't hoisted because it's part of the expression.
 

Summary

 
Thanks for reading. I hope you understood the concepts of Hoisting in JavaScript. If you have any suggestion regarding this article then please contact me.