| chapter | 27 |
|---|---|
| pageNumber | 260 |
| description | Understanding 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.
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.
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.
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
-
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.
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.
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
- Global Scope: Contains
a. - Function Scope (foo): Contains
band has access toa. - Function Scope (bar): Contains
cand has access toaandb.
Hoisting is a behavior in JavaScript where variable and function declarations are moved to the top of their containing scope during the creation phase.
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");
}hoistedVaris declared but not initialized during the creation phase, so it isundefinedwhen accessed.hoistedFunctionis fully hoisted and can be called before its declaration.