Execution context is like the environment where your JavaScript code runs. It helps the computer understand what variables and functions are available at any moment.
0
0
What execution context is in Javascript
Introduction
When you want to understand how JavaScript runs your code step-by-step.
When debugging to see why a variable is undefined or has a certain value.
When learning how functions remember their variables.
When you want to know how JavaScript handles multiple pieces of code running one after another.
Syntax
Javascript
// Execution context is not written in code but happens behind the scenes // It includes things like: // - Variable environment // - Scope chain // - This value
Execution context is created automatically by JavaScript when code runs.
There are different types: global and function execution contexts.
Examples
When this code runs, JavaScript creates a global execution context first, then a function execution context when greet() runs.
Javascript
console.log('Hello'); function greet() { let name = 'Alice'; console.log(name); } greet();
Here, a new lexical environment is created inside the if statement to handle the variable y.
Javascript
let x = 10; if (x > 5) { let y = 20; console.log(x + y); }
Sample Program
This program shows how JavaScript creates a function execution context when sayHello runs, then goes back to the global context.
Javascript
function sayHello() { let greeting = 'Hello'; console.log(greeting); } sayHello(); console.log('Done');
OutputSuccess
Important Notes
Each execution context has its own set of variables and functions.
Execution contexts are stacked; the last one created runs first (like stacking plates).
Understanding execution context helps you avoid errors with variable scope.
Summary
Execution context is the environment where JavaScript code runs.
It controls what variables and functions are accessible at any time.
There are global and function execution contexts.