- Synchronous single-threaded means that it can execute one command in order at a time.
Execution context has two components:
- Memory component is also known as a variable environment which contains variables and functions as the key-value pairs.
When you understand this core thing everything makes sense.
How is Execution Context created and how is the program run?
- Phase 1 (Creation Phase) is allocating memory to the variables and functions. For all variables value stored in memory is undefined and functions are copied in memory.
As we can see from the picture, in Phase 1 value for variables a, sum1, and sum2 is undefined and the code for function add is copied in the memory.
In Phase 2 first value of 20 is stored in a.
When function add is invoked the first time, a new local execution context is created for that code block. It goes through two phases: the creation and execution phases. When the function reaches the keyword return local execution context is destroyed and the result is stored in variable sum1. The same thing happens when we invoke the function again.
- Global Execution Context (GEC)
- Function Execution Context (FEC)
Keyword return returns control of the program back to the place where a function was invoked. Whenever a function is invoked, a new execution context is created on the top of GEC. When a function returns a value, the function execution context is popped out of a stack and the control goes back to the Global Execution Context where it is left.
Call stack maintains the order of execution of “Execution Contexts” and it is also known as:
- Execution Context stack
- Program stack
- Control stack
- Runtime stack
- Machine stack
Hoisting only works for function declarations, not expressions. Here is an example of a function expression where the code execution will break.
subtract will be hoisted as a variable, not as a function. And with variable hoisting, its value will be set to undefined.
Keyword this is a pointer to a global object. If you try to log in developer tools console, you’ll get that this is equal to the window.
This blogging platform is created specifically for software developers. We aim to support many more programming languages and development environments but for that, we need your support. If you like this blogging platform, consider using it to write your blogs.
We tried to make your experience of creating blog as painless as possible soSign in and give it a try.