HOW JAVASCRIPT WORKS AND EXECUTION CONTEXT
https://www.youtube.com/watch?v=iLWTnMzWtj4&list=PLlasXeu85E9cQ32gLCvAvr9vNaUccPVNP&index=3&ab_channel=AkshaySaini

- Everything happens in a Execution Context inside a Javascript.
- When we run this code a global execution context is created. This is created in two phases
- Memory creation phase: This allocated memory to functions and variables. When I allocated the value it store a spatial value which is know as undefined. But in case of function is stores the whole code in a memory.
- Code execution phase: Now JavaScript run through the code once again and executes the code. Now what JavaScript does is when it allocated a undefined value to n in the first phase that is the Memory creation phase now it allocates the actual value like it allocates n = 2. When it goes from line to 1-2 it does nothing because the function is declared here and not invoked so there is nothing to be executed here.
But as it hits the line number 6 there is a function invoke so now JS makes a new execution context think of it as a sub process inside a global process and yes it is a execution context inside a global execution state. So this new execution will do all the things we discussed that is memory creation and code execution for the function.
When the function hits return state it tells the code to go and hit the execution of the next line in the code. And as soon as the return hit hit it will delete the execution context of the function.
Finally after hitting the return statement of the last function it will delete the execution context of the function after all the code is executed it will also delete the global execution context

Phase 1: Memory Creation Phase

Phase 2: code execution - When the fist function is invoked

Final Function execution