Skip to content

Types of Execution Context

JavaScript has three types of execution contexts: global, function, and eval. Each is created at different times and serves a different purpose.

Created when the JavaScript script first runs:

// This runs in the global execution context
const globalVar = 'I am global';
function outer() {
// Function execution context
}
  • Only one global context exists
  • It’s the default context
  • In browsers: this = window
  • In Node.js: this = global
  • Remains active until the program ends

Created every time a function is called (not defined):

function first() {
// Function EC for first()
function second() {
// Function EC for second()
}
second();
}
first();
// Creates: Global EC → first() EC → second() EC
  • Each function call creates a new context
  • Multiple function contexts can exist (stacked)
  • Each has its own variable environment and this binding

Created when code is executed inside eval():

eval('var x = 10;'); // Creates an eval execution context
  • Rarely used (and discouraged)
  • Has its own scope
  • Can access the enclosing scope
flowchart LR
A["Script Starts"] --> B["Global EC Created"]
B --> C["Function Called"]
C --> D["Function EC Created"]
D --> E["Function Returns"]
E --> F["Function EC Destroyed"]
F --> G["More Functions?"]
G -->|Yes| C
G -->|No| H["Program Ends"]
H --> I["Global EC Destroyed"]
TypeCreated WhenHow Many Can Exist
GlobalScript starts1
FunctionFunction is calledMultiple (stacked)
Evaleval() is calledMultiple