Execution Context
IntermediateWhat JavaScript sets up before running your code, and what happens each time a function is called.
Empty
Empty
Empty
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
2 min read
On this page
What is an execution context in JavaScript?
An execution context is the environment JavaScript creates to run a piece of code. There is one
global context for the script, and a new one for every function call. Each context holds the
variables its code can use, the value of this, and a link to the scope the code was written in.
Contexts live on the . Calling a function pushes its context on top; returning pops it off. The context on top is the one that is running.
How does JavaScript create and run an execution context?
Every context goes through two phases:
- Creation. JavaScript reads ahead and sets up memory.
varvariables start asundefined, function declarations are stored whole, andlet/constare reserved but not usable yet. - Execution. The code runs line by line, assigning values and calling functions.
Picture each call as a fresh page in a notebook. The function writes its own variables on that page, and the page is thrown away when the function returns.
What mistakes do people make with execution contexts?
Mini example
function square(n) { const result = n * n; return result;} console.log(square(3)); // 9console.log(square(4)); // 16: a fresh context with its own n and resultQuick check
Over the whole mini example, how many separate result variables are created?
Further reading
All steps as text
- Step 1:
Before any code runs, JavaScript creates the global execution context and pushes it onto the call stack. It starts in the creation phase.
- Step 2:
In the creation phase, memory is set aside for each declaration.
var greetingstarts as undefined.Line 1
- Step 3:
The function declaration is stored whole:
greetpoints to a function object in the heap.Lines 3–6
- Step 4:
var resultis created too, also as undefined. Nothing has run yet.Line 8
- Step 5:
Memory is ready, so the global context moves to the execution phase and runs the code from the top.
- Step 6:
Line 1 runs:
greetingis assigned 'Hello'.Line 1
- Step 7:
Calling
greet('Ada')creates a new function execution context, with its own environment, on top of the stack.Line 8
- Step 8:
greet's creation phase begins. The parameter
namereceives the argument 'Ada'.Line 3
- Step 9:
var messageis created in greet's own environment, starting as undefined.Line 4
- Step 10:
greet's context switches to the execution phase and runs its body.
Line 4
- Step 11:
greeting + ', ' + nameis evaluated, andmessagebecomes 'Hello, Ada'.Line 4
- Step 12:
return messagehands 'Hello, Ada' back. greet's context is popped off the stack, and its environment is discarded.Line 5
- Step 13:
Back in the global context,
resultreceives the returned value.Line 8
- Step 14:
console.log(result)prints Hello, Ada.Line 9
- Step 15:
The script has finished. The global context stays until the page or program closes.