/  Technology   /  JavaScript Execution Context

JavaScript Execution Context

 

This is an extremely important topic for JavaScript developers or anyone interested in gaining a deeper understanding of JavaScript’s working process.

Here, in this section, we will learn about the JavaScript execution context, its types, execution stack, and how the execution context is created, as well as all about the execution phase of JavaScript. We will discuss each point in turn. Let’s begin with the introduction.

What actually is an Execution Context

The execution context describes the internal workings of a program. The JavaScript execution context is the environment in which JavaScript code can be executed. It is the execution context that determines which code section has access to functions, variables, and objects. In the execution context, the specific code is parsed line by line and variables and functions are stored in memory. A context of execution is similar to a container that stores variables, and the code is evaluated and then executed. As a result, it is the execution context that provides an environment in which the specific code can be executed.

Types of Execution Context

The types of execution context in JavaScript are:

  • Global Execution Context/GEC
  • Functional Execution Context/FEC
  • Eval Execution Context

Global Execution Context

The GEC / Global Execution Context is also known as the base/default execution context. JavaScript code that does not reside in any function will be present in the global execution context. This is the reason behind its name ‘default execution context’, as it is the context in which the code is executed when the file is first loaded in a web browser. Two tasks are performed by GEC:

  • Firstly, it creates a global object for Node.js and a window object for browsers.
  • The second step is to reference the Windows object by using the keyword ‘this’.
  • In order to store variables and function references, create a memory heap.
  • Afterward, it stores all of the function declarations in the memory heap area and all variables in the GEC with initial values of ‘undefined’.

In light of the above discussion, it is important to note that the Global Execution Context is only one in every JavaScript code. This is because the JS engine is single-threaded, and therefore only one global environment is possible.

Functional Execution Context

Functional Execution Code, or FEC, is the type of context created by the JavaScript engine when it detects a function call. The FEC can consist of more than one execution context, unlike the GEC, since each function has its own execution context. The FEC has access to the entire code of the GEC, but the GEC cannot access the entire code of the FEC. A function call is initiated during the execution of the GEC code, and when found by the JS engine, a new FEC for that function is created.

Eval Function Execution Context

Any JS code that is executed within the eval function creates and maintains its own execution context. JavaScript developers do not use the eval function, but it is a part of the execution context.

 

Leave a comment