Execution context is one of the most fundamental concepts in JavaScript — it's the environment in which JavaScript code is evaluated and executed.
What Is an Execution Context?
Every time JavaScript runs code, it creates an execution context — a wrapper that holds information about the current code being run: what variables are available, what this refers to, and the outer environment it has access to.
There are three types:
Global Execution Context (GEC) — created once when the script first runs
Function Execution Context (FEC) — created every time a function is invoked
Eval Execution Context — created inside eval() calls (rarely used, mostly avoided)
Every execution context goes through two distinct phases:
Phase 1: Creation Phase (Memory / Hoisting Phase)
Before any code runs, the JavaScript engine scans the code and:
- Creates the Variable Environment (stores var declarations, initialized to undefined)
- Creates the Lexical Environment (stores let/const declarations in the Temporal Dead Zone)
- Stores function declarations in memory in their entirety (fully hoisted)
- Determines the value of
this - Creates a reference to the outer environment (the scope chain)
This is why hoisting happens — variables and functions are placed into memory before code executes line by line.
Phase 2: Execution Phase
The engine runs the code line by line, assigns values, calls functions (which create new FECs), and so on.
The Call Stack
To keep track of all the contexts, including global and functional, the JavaScript engine uses a call stack. A call stack is also known as an 'Execution Context Stack', 'Runtime Stack', or 'Machine Stack'.
JavaScript is single-threaded, so it processes one execution context at a time using a call stack (a LIFO data structure). The GEC sits at the bottom; each function call pushes a new FEC on top; when a function returns, its context is popped off.
Top comments (0)