There are statements as well like, only the variable declarations are hoisted and initialisations are not. All these explanations are explained conceptually. Things work different in reality.
Now the fun part is global execution context does not start executing code line by line, instead it has two process before it start the execution of code.
1. The Creation Process
2. The Execution Process
Both process do their job very differently. Let's just discuss the creation process and reveal the secret of this blog.
- Step 1: Code starts executing in GEC.
- Step 2: The creation process started. It looks for all the variable and functions register them in global scope with the value of 'undefined' to variable and assigned whole function body to the function name.
- Step 3: The execution process started and actually assigned the value of
a. The value of
ais now replaced from
undefinedto a string
Every variable is undefined
Inside the highlighted red part you can see that
a in global scope with a default value of
aFunction is also added in global context with its whole body.
You can also try and debug the code in your browser, try with ES6 arrow functions and check if it is undefined or not. The information is taken from so many videos and articles and shared it here. Share your ideas in comments below.