The sealed container
Everything in JS runs inside an Execution Context.
- Two components: Memory (Variable Environment) + Code (Thread of Execution).
- Memory stores variables and functions as
key:valuepairs. - Code runs one line at a time, in order.
- A Global Execution Context is created the moment a program runs.
Sync + single-threaded
Why JS runs line by line.
- Synchronous — one command executes at a time.
- Single-threaded — there is one call stack, one thread of execution.
- JS cannot move to the next line until the current one finishes.
- Async behaviour comes from Web APIs + the callback queue + the event loop — not from the language itself.
The two phases
Memory Creation → Code Execution.
- Phase 1 — Memory Creation. Scan the code, allocate memory.
- Variables get
undefined; function declarations get their entire code. - Phase 2 — Code Execution. Run line by line, assign real values, invoke functions.
- Hoisting is a consequence of Phase 1 — not a physical movement of code.
Function invocations
Each call creates a fresh context.
- Invoking a function creates a brand new execution context pushed on top of the stack.
- That context has its own two phases (Memory + Code) and its own variables.
returnsends a value back and destroys the function’s context.- Multiple contexts can exist at once; only the top one is actively running.
Sequential execution in action
Classic output traces.
console.log("A"); console.log("B"); console.log("C")→ A → B → C (strict order).var x = 1; console.log(x); x = 2;→ prints 1 then the assignment runs.- Re-declaring
var xin the same scope is silently allowed; only the last value wins. - Function declarations are “skipped” during execution — already stored in Phase 1.
Comments
Comments are disabled in this environment. Set
PUBLIC_GISCUS_REPO,PUBLIC_GISCUS_REPO_ID, andPUBLIC_GISCUS_CATEGORY_IDto enable.