Skip to content
YouTurkey Tech
All JavaScript topics

Execution Context

Intermediate

What JavaScript sets up before running your code, and what happens each time a function is called.

Call stack

Empty

Scopes

Empty

Heap

Empty

Console

Empty

0/15Press Play, or step through with the arrows, to begin.
Step 0 of 15

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:

  1. Creation. JavaScript reads ahead and sets up memory. var variables start as undefined, function declarations are stored whole, and let/const are reserved but not usable yet.
  2. 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 result

Quick check

Over the whole mini example, how many separate result variables are created?

Further reading

All steps as text
  1. 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.

  2. Step 2:

    In the creation phase, memory is set aside for each declaration. var greeting starts as undefined.

    Line 1

  3. Step 3:

    The function declaration is stored whole: greet points to a function object in the heap.

    Lines 3–6

  4. Step 4:

    var result is created too, also as undefined. Nothing has run yet.

    Line 8

  5. Step 5:

    Memory is ready, so the global context moves to the execution phase and runs the code from the top.

  6. Step 6:

    Line 1 runs: greeting is assigned 'Hello'.

    Line 1

  7. Step 7:

    Calling greet('Ada') creates a new function execution context, with its own environment, on top of the stack.

    Line 8

  8. Step 8:

    greet's creation phase begins. The parameter name receives the argument 'Ada'.

    Line 3

  9. Step 9:

    var message is created in greet's own environment, starting as undefined.

    Line 4

  10. Step 10:

    greet's context switches to the execution phase and runs its body.

    Line 4

  11. Step 11:

    greeting + ', ' + name is evaluated, and message becomes 'Hello, Ada'.

    Line 4

  12. Step 12:

    return message hands 'Hello, Ada' back. greet's context is popped off the stack, and its environment is discarded.

    Line 5

  13. Step 13:

    Back in the global context, result receives the returned value.

    Line 8

  14. Step 14:

    console.log(result) prints Hello, Ada.

    Line 9

  15. Step 15:

    The script has finished. The global context stays until the page or program closes.