DEV Community

Abdul Haseeb
Abdul Haseeb

Posted on

Hoisting in JavaScript: Most Asked Topic in JOb Interviews

Image description

no trash talking let's get straight in

look at this example:

see function is called before it is declared,
but JavaScript automatically puts the declaration at the top of the script this is called hoisting it is simple:

Image description

remember that only declaration will be hoisted not the initialization.

var myVariable will be hoisted and it contains the value undefined after assignment it contains the value 42
so here "var myVariable" is hoisted nut "= 42" is not.

Image description

here when we declare the function using var you can see that calling sayHello() before intialization throws an error because intialization is not hoisted "Var sayHello" existed but we did'nt assigned a function to it as initialization is not hoisted so it throws an error.

Image description

Top comments (0)