1 2 3 4 |
>> console.log(something) ReferenceError: something is not defined >> someFunction() ReferenceError: someFunction is not defined |
variable hoisting
1 2 3 |
>> console.log(something) >> var somthing = 'something' undefined |
function hoisting
1 2 3 4 5 6 7 |
/* This code needed to execute in global execute context */ someFunction() function someFunction(){ console.log('print something'); } print something |
How hoisting work?
WRONG the variable or function are moving to the top
WRONG the variable or function are declared first and assign value later
CORRECT execution context was created in two phases:
- the create phase: create global object and outer environments, as the parser runs through, it set up the variables and the functions in memory(recognize where they placed, existed in memory), it’s available to access them, BUT variable is not like functions which are placed entirely in memory, context knows the value only when it end up executing the code, when it access that variable, it got a placeholder undefined.
- the execution phase