Photo by Juanjo Jaramillo on Unsplash
Execution Context in JavaScript
How JavaScript Program is being Executed
Table of contents
No headings in the article.
In JavaScript, the execution context is an abstract concept that defines the environment in which the JavaScript code is executed. It contains information about the current state of the program, such as the value of variables and functions, the call stack, and the value of this
keyword.
Each time a function is called in JavaScript, a new execution context is created for that function. This context includes a reference to the outer or parent execution context, which represents the environment in which the function was defined. When the function returns, the execution context is popped off the call stack and control is returned to the previous context.
There are three types of execution context in JavaScript:
Global Execution Context: This is the outermost execution context and is created when the JavaScript code is first loaded into the browser. It contains information about the global scope, including global variables and functions.
Function Execution Context: This is created when a function is called and contains information about the local scope of the function, including local variables and parameters.
Eval Execution Context: This is created when the
eval()
function is called and contains information about the scope of the code being evaluated.
Each execution context has its own variable environment, which includes the variables declared within that context as well as any variables declared in its parent contexts. This variable environment is used to resolve variable references within the context.
Understanding execution context is important in JavaScript because it helps developers understand how the code is executed and how variables and functions are scoped within their programs.