What is Execution Context
An execution context is the environment in which JavaScript code is evaluated and executed. It contains all the information the engine needs to run the code: the variable scope chain, the this value, and references to the outer environment.
Think of it as a blueprint or container that holds everything needed for a specific piece of code to run.
// Global Execution Context is created when the script starts
var name = 'Alice'; // Variable declaration in global context
function greet() {
// Function Execution Context is created when greet() is called
console.log(`Hello, ${name}`);
}
greet(); // Creates a new execution context for greet
Every time a function is called, a new execution context is created for that function invocation. This is how JavaScript tracks which variables and scopes are active at any given moment.
Execution Context Types
There are three types of execution contexts in JavaScript:
1. Global Execution Context
Created when a JavaScript file is first loaded. There is only one global execution context. It:
- Creates the global object (
windowin browsers,globalin Node.js) - Sets
thisto reference the global object - Sets up the outer environment (null for global)
2. Function Execution Context
Created every time a function is invoked. Each function call gets its own execution context with its own variables, parameters, and scope chain.
function outer() {
var x = 10;
function inner() {
var y = 20;
console.log(x + y); // inner has its own execution context
}
inner(); // inner's execution context references outer's via scope chain
}
outer(); // outer's execution context references global
3. Eval Execution Context
Created when code is executed inside an eval() function. This is rarely used and generally discouraged.
eval('var z = 30;'); // Creates an eval execution context
Creation and Execution Phases
Each execution context goes through two distinct phases:
Creation Phase
Before any code is executed, the engine processes declarations:
- Variable declarations are hoisted and initialized to
undefined - Function declarations are hoisted and stored in memory
- The
thisbinding is determined - The scope chain is established
console.log(x); // undefined (hoisted, not yet assigned)
var x = 5;
console.log(add(2, 3)); // Works! Function is hoisted
function add(a, b) {
return a + b;
}
Execution Phase
The engine executes the code line by line:
- Variables are assigned their actual values
- Functions are called (creating new execution contexts)
- The
thisvalue may change based on how functions are called
var x = 10; // Execution phase: x is assigned 10
function multiply(a, b) {
return a * b;
}
var result = multiply(x, 5); // Execution phase: function called, new context created
Visual Example
// Creation Phase
var a = undefined; // hoisted
function foo() {} // hoisted and stored
// Execution Phase
a = 10; // assigned
foo(); // called, new context created
Practice Problems
Create a reusable React component implementing Execution Context. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for Execution Context using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize Execution Context for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. What is an execution context?
2. How many global execution contexts exist in a JavaScript program?
3. During which phase are variable declarations initialized to `undefined`?
4. What happens to function declarations during the creation phase?
Flashcards
Question
What is an execution context?
Click to reveal answer
Answer
An environment that holds all the information needed for code execution: variable scope, `this` binding, and the scope chain.
Question
What are the three types of execution contexts?
Click to reveal answer
Answer
Global, Function, and Eval execution contexts.
Question
What happens during the Creation Phase?
Click to reveal answer
Answer
Variable declarations are hoisted (initialized to `undefined`), function declarations are stored in memory, `this` binding is determined, and the scope chain is established.
Question
What is the difference between Creation and Execution phases?
Click to reveal answer
Answer
Creation phase processes declarations and sets up the environment; Execution phase runs code line by line, assigning values and calling functions.
Question
What is Execution Context?
Click to reveal answer
Answer
Execution Context is a key concept in frontend development.
Revision Notes
Key Takeaways
- 1.Every JavaScript program has exactly one global execution context
- 2.Function calls create new execution contexts
- 3.Hoisting happens during the Creation Phase
- 4.Function declarations are fully hoisted, variable declarations are hoisted but initialized to undefined
- 5.The scope chain connects execution contexts together
Interview Tips
- •Explain the difference between the creation and execution phases clearly
- •Use examples to demonstrate hoisting behavior
- •Be prepared to trace through code and identify when execution contexts are created
- •Discuss how the scope chain relates to closure behavior
Cheat Sheet
Execution Context Cheat Sheet
What is it?
- Environment where code is evaluated and executed
- Contains variable scope,
thisvalue, and scope chain
Types
- Global: Created on script load, one per program
- Function: Created on each function call
- Eval: Created by eval() (rarely used)
Phases
Creation Phase:
- Variable declarations hoisted →
undefined - Function declarations hoisted → stored in memory
thisbinding determined- Scope chain established
- Variable declarations hoisted →
Execution Phase:
- Variables assigned actual values
- Functions called (new contexts created)
- Code executed line by line