Execution Context in JavaScript
In JavaScript, an execution context is an environment where the code is evaluated and executed. It is a fundamental concept that helps manage the scope and behavior of variables and functions.
Types of Execution Context
There are three main types of execution contexts in JavaScript:
- Global Execution Context: This is the default context where the code starts execution. It creates a global object (e.g.,
windowin browsers) and sets up the global scope. - Function Execution Context: Created whenever a function is invoked. Each function has its own execution context.
- Eval Execution Context: Created when code is executed inside the
evalfunction.
Phases of Execution Context
Each execution context goes through two phases:
Creation Phase: In this phase, the JavaScript engine sets up the environment for the code to be executed. It involves:
- Creating the
thisbinding. - Setting up the scope chain.
- Creating the variable object (variables, functions, and arguments).
- Creating the
Execution Phase: In this phase, the JavaScript engine executes the code line by line.
Example of Execution Context
Here's an example to illustrate how execution contexts work:
var globalVar = "I am a global variable";
function outerFunction() {
var outerVar = "I am an outer variable";
function innerFunction() {
var innerVar = "I am an inner variable";
console.log(globalVar); // Accesses global variable
console.log(outerVar); // Accesses outer variable
console.log(innerVar); // Accesses inner variable
}
innerFunction();
}
outerFunction();
Output:
I am a global variable
I am an outer variable
I am an inner variable
Explanation
Global Execution Context:
globalVaris created and assigned the value "I am a global variable".outerFunctionis created and stored in memory.
Function Execution Context (outerFunction):
outerVaris created and assigned the value "I am an outer variable".innerFunctionis created and stored in memory.
Function Execution Context (innerFunction):
innerVaris created and assigned the value "I am an inner variable".- The
console.logstatements access variables from their respective scopes.
Scope Chain and Lexical Environment
The scope chain is a list of all the variable objects that the currently executing code has access to. The lexical environment is the environment in which the code is written, and it determines the scope chain.
Example of Scope Chain
var a = 10;
function foo() {
var b = 20;
function bar() {
var c = 30;
console.log(a + b + c); // Accesses variables from all scopes
}
bar();
}
foo();
Output:
60
Explanation
- Global Scope: Contains
a. - Function Scope (foo): Contains
band has access toa. - Function Scope (bar): Contains
cand has access toaandb.
Hoisting
Hoisting is a behavior in JavaScript where variable and function declarations are moved to the top of their containing scope during the creation phase.
Example of Hoisting
console.log(hoistedVar); // Output: undefined
var hoistedVar = "I am hoisted";
hoistedFunction(); // Output: I am a hoisted function
function hoistedFunction() {
console.log("I am a hoisted function");
}
Explanation
hoistedVaris declared but not initialized during the creation phase, so it isundefinedwhen accessed.hoistedFunctionis fully hoisted and can be called before its declaration.