Hoisting in JavaScript: Most Asked Topic in JOb Interviews
Abdul Haseeb
Posted on January 19, 2024
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:
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.
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.
Posted on January 19, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.