Understanding Execution Context in JavaScript.

Understanding Execution Context in JavaScript.

Execution Context:

Execution context (EC) is defined as the environment in which the JavaScript code is executed. By environment, I mean the value of this, variables, objects, and functions JavaScript code has access to at a particular time.

All JavaScript code needs to run in an environment and the environment is called Execution Context.

Default Execution Context is always Global Context.

The Global Execution Context is for variables and functions that are not inside of any function.

Execution Context Stack (ECS):

It's a stack of Execution Context. ( Last In First Out ) to store all the execution stacks created during the life cycle of the script.

Global Execution Context is present by default at the bottom of the Execution Context Stack.

While executing the global execution context code.

Each function gets its own execution context & pushed to the top of the execution context stack.

JS engine executes the function whose execution context is at the top of the execution context stack.

Once all the code of the function is executed, JS engines pop out that function’s execution context and start’s executing the function which is below it.

Explanation:

image.png

image.png

image.png

image.png

image.png

image.png

image.png