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.
Top comments (0)