JavaScript Execution ContextThe topic is very much important for those who are JavaScript developers or want to have a deep knowledge of the working process of JavaScript. Here, in this section, we will learn and understand the execution context of JavaScript, where we will discuss what it is, its types, execution stack, how the execution context is created and all about the execution phase. We will discuss each point one by one. Let's begin with the introductory part first. What actually is an Execution Context Execution context is the concept for describing the internal working of a code. In JavaScript, the environment that enables the JavaScript code to get executed is what we call JavaScript Execution Context. It is the execution context that decides which code section has access to the functions, variables, and objects used in the code. During the execution context, the specific code gets parsed line by line then the variables and functions are stored in the memory. An execution context is similar to a container that stores variables, and the code gets evaluated and then executed. Thus, it is the execution context that provides an environment for the specific code to get executed. Types of Execution ContextThe types of execution context in JavaScript are:
Begin to discuss each one by one: Global Execution ContextGEC / Global Execution Context is also called the base/default execution. Any JavaScript code which does not reside in any function will be present in the global execution context. The reason behind its name 'default execution context' where the code begins its execution when the file first loads in the web browser. GEC performs the two following tasks:
With the above introduction, one should understand that the Global Execution Context is only one in every code because the JS engine is single-threaded, and thus, only one global environment is possible for executing the JavaScript code. Functional Execution ContextFEC or Functional Execution Code is that type of context which is created by the JavaScript engine when any function call is found. Every function has its own execution context, and thus unlike GEC, the FEC can be more than one. Also, FEC can access the entire code of the GEC, but it is not possible for GEC to access all the code of the FEC. During the GEC code execution, a function call is initiated, and when found by the JS engine, it creates a new FEC for that specific function. Eval Function Execution ContextAny JS code that gets executed within the eval function creates and holds its own execution context. However, the eval function is not used by the JavaScript developers, but it is a part of the Execution Context. Execution StackThe execution stack is also known as Call Stack. The stack is the data structure that stores the values in the form of LIFO (last in, first out). Similarly, an execution stack is a stack that carries track of all the execution contexts developed during the script life cycle. A JavaScript developer must be known of the fact that JavaScript works as single-threaded where it is capable of executing a single task in the web browser at a time. Thus, for other actions, functions, and events, a stack is created and is known as the Execution Stack. At the bottom of the execution stack, GEC resides, and it is present by default in the stack. So, when beginning a JS code execution (i.e., during GEC execution), when any function is present in the code, and the JS engine searches it, it instantly creates a function execution context (FEC) for that function and pushes it on the top of the execution context stack. The particular execution context which is available at the top of the execution context stack will always get executed by the JS engine first. As soon as the execution of all the code is done, the JS engine pops out the function's execution context and then moves towards the next and so on. Generally, when the script loads in the browser, the first element will be the global execution context. But when a function execution is detected, the execution context is created and gets virtually placed on the top of the GEC. The process continues until the execution of the whole code gets completed. In order to understand the working process of the execution stack, let's consider an example code given below: It is an example code to understand its working. Explanation:
In this way, the execution of the execution stack is performed. Creating an Execution ContextAn execution context is created first, and then it is managed. The creation of the execution context is performed in two approaches: Creation PhaseThe creation phase is the one in which the JS engine invokes a function but does not begin its execution. In this phase, the JS engine begins its compilation phase and scans the particular function code for compiling it but does not execute the code. Creation of the Execution Context is the responsibility of the JavaScript engine, and it creates it by performing the following described tasks: Task 1: Creation of the Activation object/Variable Object: One of the special object of JavaScript that is like a container that holds all the information of the function arguments, variables as well as inner functions declaration. With this, it does not have the dunder proto property. Task 2: Creation of the scope chain: After completing task 1, the JS engine initializes the scope chain. A scope chain is a list that has all the variables objects within which the current function exists. A scope chain also has the variable object of the GEC and carries the current function variable object.
Let's try to understand the creation of the Activation object with the help of the below example: Example Code 1 Now, the JS engine creates an executionContextObj for example code 1 after the invocation of test () and before executing its code. It can be seen in the below code: The Activation object contains the argument object that further contains the details about the arguments of the function. It carries the property name for every function and variables that are declared within the current function. In our case, the Activation object, for example, code 1will be: Explanation:
So, in this way, the creation phase works. Execution PhaseThe execution phase is the next phase after finishing the creation phase. An execution phase is the one where the JS engines scan through the function in the code once again, i.e., one more time for updating the variable object with the values of the variables and then run the code. Let' see the execution stage or the complete code for the example which we have discussed above: Example Code 2 Firstly, the above code gets loaded in the browser. After that, the JS engine begins its compilation phase in order to create the execution objects. In the compilation phase, the JS engine manages and handles the declarations only and does not care about the values. Now, in the execution phase, the following steps will be performed as described per line:
So, in this way, the working of both the creation phase, as well as the execution phase, takes place. GEC object after the creation phase stage In the above explanation, we saw how the execution stack is created via the execution and creation phase for example code 2. However, we should take a deep understanding of the working of the GEC and FEC for the above-mentioned code only. Consider the below GEC object code for example code 2: As you can see above, no code is left, the JS engine moves to the execution phase for scanning the function once more. The JS engine updates the value of the variable then execute the code in the following way as described below per line:
Now, let's see the GEC object after the execution phase. GEC object after Execution Phase In our example code 2:
z execution object after the compilation phase Below is the code for the FEC object after completing the compilation phase: The code after the compilation phase of the example code 2 is performed as described below:
Execution Context Object of z after the execution phase In example code 2:
So, this is how the execution context is created and is defined. Global Execution Context Vs. Function Execution ContextThere are the following differences between the two:
Next TopicJavaScript querySelector |