Hoisting in Javascript

Table of contents

Hoisting

basically in hoisting, a variable is called before it is intialised or declared. yeah, there is a difference between initialising a variable and declaring a variable.

Hoisting is one of the most famous topic about which interviewers ask.

console.log(a)
var a = 10;

now you might think that how is this even possible. so, now let's open up this code and understand how will javascript interpret and run this code.

  • first the JS will took the "var a" and initialised it on top before executing any other thing.

  • so, now "a" is initialised we just have to declare it to set it's value but we know that if we don't give any value to a variable then it will give us undefined when we will call it.

      var a;
      console.log(a)
      a = 10;
    

    now the javascript will read the previous code as shown above

  • if you have any doubt in it feel free to comment or reach me out at github.com/oscaroptics