+2 votes

1 Answer

+2 votes
by (13.6k points)
selected by
 
Best answer

Javascript code

In Javascript, an execution context is an abstract container that holds information about the environment that a piece of code is being executed in. For every function call, a new execution context is created and added to the execution stack; a container that keeps information about the different execution contexts.

The Execution Stack

Image source: https://blog.yipl.com.np/backend-knowledge-sharing-19-8ba52645d927

The global execution context is the default execution context created before the JS engine starts run through any code.

On every function call in Javascript, a new execution context is created and added to the execution stack. It is the JS engine's job to configure it and prepare for the execution. 

You might have heard terms like scope, scope chain and this.

Here is why it is important for the execution context to have them:

Scope: Every execution context needs to define its scope; it needs to decide which functions and variables should have direct access to it.

Scope chain: Aside from the individual scopes of the execution context, there are also references to an execution context's outer and global scopes. This chain of reference is known as the scope chain.

You can access global variables from local scopes, but cannot access local variables from global scopes.

this: This (this keyword) is a special variable that allows access to an object to which the current code is being executed. In the global execution context, this allows access to the Window object.

Final points to answer your question

The global execution context in Javascript is an abstract container created by default by the Javascript  engine to hold information about the global environment within which a code is being executed. 

An execution context is created on every new function call in Javascript. Memory is allocated, scope and scope chains are defined and the value of this is determined.

...