JavaScript Execution Context Interview Questions and Answers

Review how JavaScript creates execution contexts, environments, and bindings when code runs.

Frequently Asked Questions

Important questions are highlighted in the sections below; select one to jump to its answer.

Theoretical JavaScript Interview Questions

Every time JavaScript runs code, it does so inside an execution context - a wrapper that contains the local variables, the scope chain for looking up outer variables, and the value of this. There's one global execution context when your script starts, and a fresh one is created for each function call. Understanding execution contexts is key to understanding hoisting, closures, and why this behaves the way it does.

Every item on the call stack is an execution context - they're two sides of the same coin. When you call a function, a new execution context is created and pushed onto the stack, and the engine always executes whatever's at the top. When the function finishes, that context is popped and the engine resumes the one below it. This is exactly what you see in a stack trace when an error is thrown.

An execution context is the specification model for evaluating code. It includes lexical and variable environments, the outer environment used for name lookup, and a this binding where applicable. A script has a global context and function calls create function contexts. In a browser script, the global object is usually window; modules and Node.js have different global bindings, so an execution context should not be equated with window.

Before any code runs, the engine goes through a creation phase: it scans for all var declarations and initializes them to undefined, stores function declarations in full, establishes the scope chain, and sets up the this binding. Then in the execution phase it runs your code top to bottom, assigning real values. This two-pass behavior is exactly what causes hoisting - var and function declarations are processed early, before any assignment code runs.

An execution context is the runtime environment in which code is evaluated, including its lexical environment and relevant this binding. Function calls and script or module evaluation create contexts.

It is the context established for a script or module's top-level code. In classic browser scripts, top-level var and function declarations may become global-object properties, unlike module bindings.

A call creates a context with its parameters, local bindings, lexical environment, and this value determined by the call form. It is pushed while the function runs and removed when it returns or throws.

A lexical environment associates identifiers with bindings and links to an outer environment for lookup. Closures retain access to relevant environments after their creating function finishes.

For regular functions, this depends on how the function is called: as a method it is generally the receiver, while plain calls follow strict-mode or environment rules. Arrow functions do not create their own this.

An arrow function does not create its own `this` binding. It uses `this` from the surrounding scope, and calling it as an object method does not replace that value.

A closure is a function with access to its surrounding lexical environment. Needed bindings remain reachable while the closure can use them, even after the outer function returns.

A plain call to a regular function in strict mode has this equal to undefined. Non-strict classic-script code may instead coerce it to the global object.

Lookup checks the current lexical environment, then follows outer links until it finds a binding or reaches the end. An unresolved identifier normally causes ReferenceError when evaluated.

Declarative environment records store bindings directly, as in most lexical scopes. Object environment records are backed by an object and appear in contexts such as some global scripts.

Top-level this in an ES module is undefined, unlike the global-object behavior often seen in classic browser scripts. Modules also have their own lexical scope.