Execution Context has two phases:
– Creation phase: sets up functions, variables inside of memory
– Execution phase: It runs your code
1 2 3 4 5 6 7 8 |
function b() { console.log('Called b!'); } b(); console.log(a); var a = 'Hello World!'; console.log(a); |
output:
Called b!
undefined
Hello World!
Basically after the creation phase, it starts executing.
Function as a whole was in memory. So executing it gives us:
Called b!
Then when we tried logging a, a is indeed in memory, but its value is undefined, which was set in the Creation Phase.
So it displays ‘undefined’.
Then we go down to the next line and sees that a is set to ‘Hello World!’.
Then we go down one line further and logs it. This time, a is set to a string, and we display that string.