Execution context (EC) is defined as the environment/scope in which JavaScript code is executed.
By environment means the value of this, variables, objects, and functions JavaScript code has access to, constitutes it’s environment.
In JS Environmentis a data structure that provides that storage space to variables.
-
Global execution context (GEC)The default environment in which code is executed for the first time.Functional execution context (FEC)Whenever the flow of execution enters a function body.Eval: Execution context inside eval function.
The JavaScript interpreter in a browser is implemented as a single thread.
What this actually means is that only 1 thing can ever happen at one time in the browser, with other actions or events being queued in what is called the Execution Stack.
-
- Single threaded.
- Synchronous execution.
- 1 Global context.
- Infinite function contexts.
- Each function call creates a new execution context, even a call to itself.
-
Creation phasewhen the function is called, but before it executes any code inside.
JS engine is in the compilation phase and it scans over the function to compile the code.- Create the
Scope Chain. - Create
variables,functionsandarguments. - Determine the value of
this.
- Create the
-
Activation / Code Execution phase- Assign values, references to functions and interpret / execute code.