In JavaScript, every piece of code runs inside something called an Execution Context. It is the environment where JavaScript code is evaluated and executed. Whenever a program runs, JavaScript creates an execution context to manage variables, functions, and the flow of execution.
Understanding execution context is very important because it explains how JavaScript processes code, manages variables, and executes functions step by step.
What is Execution Context?
Execution context is the environment in which JavaScript code runs. It determines what variables, functions, and objects are available during execution and how the code is interpreted.
Each time a JavaScript program starts or a function is called, a new execution context is created.
Types of Execution Context
JavaScript mainly has two types of execution contexts:
- Global Execution Context
- Function Execution Context
Global Execution Context
The global execution context is created when a JavaScript program starts running. It represents the default environment where all global variables and functions are defined.
There is only one global execution context in a program.
Hello World
In this example, the variable message is created in the global execution context.
Function Execution Context
Whenever a function is called, JavaScript creates a new execution context specifically for that function. This context contains the function’s variables, parameters, and local scope.
Hello, Student
When the greet() function is called, JavaScript creates a new execution context for it.
Execution Stack
JavaScript manages execution contexts using a structure called the call stack. The call stack keeps track of which function is currently running.
When a function is called, its execution context is pushed onto the stack. When the function finishes execution, it is removed from the stack.
Inside second function
In this example, JavaScript first creates the global execution context. When first() is called, a new context is created, and then another context is created for second().
Phases of Execution Context
Each execution context goes through two main phases:
- Creation Phase
- Execution Phase
Creation Phase
During this phase, JavaScript prepares the environment by allocating memory for variables and functions.
Execution Phase
In this phase, the code is executed line by line and variables receive their assigned values.
Why Execution Context is Important
Execution context helps JavaScript manage how code runs and how variables are stored in memory. It explains important behaviors such as function calls, variable scope, and the call stack.
Understanding execution context is essential for learning advanced JavaScript concepts such as closures, hoisting, and asynchronous programming.
Conclusion
Execution context is the environment in which JavaScript code runs. Every JavaScript program starts with a global execution context, and each function call creates its own execution context.
By understanding execution context and the call stack, developers can better understand how JavaScript executes code internally.
In the next tutorial, you will learn about Hoisting in JavaScript, which explains how JavaScript handles variable and function declarations before execution.

