JavaScript Execution Context
Understand how JavaScript works behind the scene.
In the world of programming, the execution context is one of the fundamental concepts that is used in many programming languages, including JavaScript. Execution context is a way to keep track of the state of a program as it runs. It is responsible for creating an environment in which the code can run and provides a way to access variables and functions. In this article, we will dive into the details of the execution context in JavaScript, including its components, creation, and usage.
What is Execution Context?
An execution context is a fundamental concept in JavaScript that refers to the environment in which the code is executed. Every time a function is executed in JavaScript, a new execution context is created. The execution context consists of a set of variables, objects, and functions that are accessible to that function during its execution.
There are two types of execution context:-
Global Execution Context: The global execution context is created when a JavaScript script first starts to run, and it represents the global scope in JavaScript.
Local/Function Execution Context: A local/function execution context is created whenever a function is called, representing the function's local scope.
The execution context can be thought of as a box that contains all the necessary information for a particular function to execute correctly. It contains three essential components:
Variable Object (VO): A variable object is an object that contains information about the variables, functions, and parameters that are currently in scope. It is created during the creation phase of the execution context and is used by JavaScript to keep track of the state of the code as it is executed.
Scope Chain: The scope chain is a list of variable objects that are in scope. It is created during the creation phase of the execution context and is used by JavaScript to look up variables and functions in the correct order when they are referenced in the code.
This Value: The
this
value is a reference to the object that the current function is a method of. It is created during the execution phase of the execution context and is used by JavaScript to provide access to object properties and methods within a function.
Execution Context Stack
JavaScript maintains a call stack to keep track of the execution context. Whenever a function is called, a new execution context is created and added to the top of the call stack. When the function completes its execution, its execution context is removed from the stack, and the control returns to the calling function.
The call stack is a Last-In-First-Out (LIFO) data structure, meaning that the last function that was added to the stack is the first function to be removed from the stack.
Phases Of Execution Context
The execution context is created in two phases: the creation phase and the activation phase.
Creation Phase: During the creation phase, the JavaScript engine creates the variable object, the scope chain, and the
this
value for the function that is being executed. The engine also initializes any variables that are declared in the function with the value undefined.Execution Phase: The execution phase is the second phase of the execution context. During this phase, the JavaScript engine assigns values to the variables that were initialized in the creation phase. It also sets the
this
value for the function and adds the function to the call stack. Once the activation phase is complete, the function is ready to be executed.
How Execution Context Works
When JavaScript code is run, the first thing that happens is that a global execution context is created. This context contains a variable object that represents the global scope, as well as a scope chain that includes the global object.
When a function is called, a new execution context is created for that function. This context has its own variable object that represents the function's local scope, as well as a scope chain that includes the function's parent variable object. The
this
value for the function is also set to the object that the function is a method of.As the function is executed, JavaScript uses the execution context to look up variables and functions in the correct order. When a variable is referenced, JavaScript first looks for it in the local scope of the current function. If it is not found there, it looks in the parent scope of the function, and so on up the scope chain until it reaches the global scope.
When the function returns, its execution context is destroyed, and the control returns to the parent execution context. This process continues until all functions have been executed and the program has finished running.
Conclusion
The execution context is a fundamental concept in JavaScript that represents the environment in which code is executed. It is made up of a variable object, a scope chain, and this value, and is used by JavaScript to keep track of the state of the code as it is executed. Understanding the execution context is crucial for writing efficient and reliable JavaScript code, and is essential for advanced topics like closures and scope.
Well, I hope you liked it and I think in this article I covered all the necessary concepts of the Execution context of JavaScript. So, if this blog added any value to your knowledge, my work is done. Thank you for reading!