Share the post


Hello All, Today in this topic we will discuss about the concept called hoisting in JavaScript. In this topic we will discuss What is Hoisting in JavaScript? Example to understand hoisting in better way.

What is Hoisting in JavaScript? 

Hoisting means use the variable before it get declared. Simply means you can able to use a variable by assigned the value to variable use it & then declare the variable. We can understand the concept in more better way by using below example.

Example : 

We will look into this concept by using three small example which shown as below :


    <h1>JAVASCRIPT : Hoisting</h1>





    console.log("Example decalre the variable after assigning the value");
    a = "this is string"; //assign the variable value
    console.log("a " + a);
    var a; // declare

    console.log("Example decalre the variable but not assigned");
    console.log("b " + b);
    var b = "this is string"; // declare

    console.log("Example decalre value using let keyword after assigning the value");
    c = "this is string"; //assign the variable value
    console.log("c " + c);
    let c; // declare


Output :


In first example, We have declared the variable after assigning the value to variable a. This concept is call as Hoisting. In next example, We have use the variable without assigning the value to the variable & declare it, therefore we get the error as undefined. We have declared the value using let keyword after assigning value to c, but in output we can clearly seen that it logs the error like ‘Uncaught ReferenceError: Cannot access ‘c’ before initialization at index.html:27‘. In this way it proves that, Hoisting can be use only for variable not for let & const keyword & the variable needs to be assigned with value otherwise it will show the error of undefined.


Leave a Comment