Execution context
Before you study the event loop and async behavior in depth, it helps to know where JavaScript runs: not “in the air,” but inside a concrete structure the engine creates called an execution context.
Why this matters
People often ask: Is JavaScript synchronous or asynchronous? Single-threaded or not? The short answers are:
- For running your JS code on the main path, execution is synchronous and single-threaded: one statement at a time, in order.
- Asynchronous behavior (timers, network, DOM events) is coordinated by the host and the event loop — that is a separate lesson.
This page is about the box your synchronous code lives in.
What is an execution context?
Everything that happens for a chunk of JavaScript runs inside an execution context. You can picture it as a single container: one place where the engine keeps the bindings it needs and runs your code line by line.
The container
Before your code runs, the engine sets up a container for this run — nothing mystical, just structure.
Memory vs code
Each execution context has two conceptual parts (names vary slightly across docs and engines, but the idea is stable):
- Variable environment (memory) — stores identifiers and their values as key–value pairs (variables, function references, etc.).
- Thread of execution (code) — the place where code actually runs, one line at a time. Only one line’s work is active in this sense at a time on the main path.
Memory vs code
The memory side is the variable environment: a map from names (keys) to values.
Synchronous & single-threaded
Putting it together:
- Single-threaded — the main JavaScript execution path does one thing at a time.
- Synchronous — the next line does not conceptually “cut in front” of the current line; ordering is sequential for that running code.
So how do fetch, setTimeout, or click handlers fit in? They schedule work outside
this straight-line run; the event loop decides what runs when the stack is clear.
That is covered in detail in
Event Loop
— no need to duplicate it here.
Recap
- An execution context is the container for a unit of JS work: memory (bindings) plus code (sequential execution).
- Variable environment ≈ where names map to values; thread of execution ≈ where lines run, one after another.
- Async is layered on top via the host and the event loop, not by “multi-threading” your source line-by-line model.
Next:Call stack & phases
— memory creation vs execution, invocations, and how frames are pushed and popped. Then continue to
Event Loop
for scheduling after synchronous code finishes.