05 / 08

What is the difference between execution context and scope?

Execution context and scope are not the same thing. Scope determines where variables are accessible, while execution context manages the environment in which code runs and memory is allocated.

When a function runs, an Execution Context is created. One of the first things that context does is look at the Scope (the script) to figure out which variables it's allowed to touch.

Scope
  1. 1

    The scope is function-based. Scope belongs to the variable access of a function. There are only two scopes in JavaScript — global and function scope.

  2. 2

    Scope is Static (Lexical). It is determined by where you write your code in the file. Once you save your .js file, the scope is set in stone.

  3. 3

    It defines the visibility and accessibility of variables.

  4. 4

    It follows the 'Scope Chain.' A child function can see variables in its parent’s scope, but a parent cannot see into a child’s scope.

  5. 5

    It is defined at author-time.

Execution context
  1. 1

    Execution context is object-based. Execution context is an abstract concept that holds information about the environment where the current code is being executed. A context of a function is the value of the this keyword for that function

  2. 2

    Execution Context is Dynamic. It is created by the JavaScript engine only when a function is actually called (invoked).

  3. 3

    It is the environment that handles the 'work.' It contains the this keyword, the variable object, and a reference to the outer scope.

  4. 4

    It is created at runtime and is destroyed once the function finishes running.

Difficulty: 5/10
Topics: execution context, scope chain, closures

Scenario Questions

0-2 years experience
  1. 1

    If you write a function that declares a variable and then call another function inside it, where will that inner function look for that variable? Walk me through the execution context and scope chain.

  2. 2

    Suppose you have a script that logs a variable before it's declared with var. What execution context is created and how does scope affect the result?

  3. 3

    When you invoke a function as a method of an object versus as a plain function, how does the execution context differ, and what impact does that have on variable resolution?

2-5 years experience
  1. 1

    You notice a variable you expected to be private is being accessed from another module. How would you investigate whether the issue is due to a misunderstanding of scope versus execution context?

  2. 2

    During a refactor, a function that relied on a global variable starts throwing ReferenceError. Explain how changes in execution context could cause that, and how you'd fix it.

  3. 3

    In a codebase using async callbacks, a closure captures a loop variable incorrectly. Explain how the execution context and scope chain interact here, and how you'd correct the bug.

5-8 years experience
  1. 1

    Our front‑end team is building a plugin system where third‑party scripts run on the same page. How would you use execution context and scope isolation to prevent plugins from leaking variables into the host app?

  2. 2

    We have a large SPA that creates many nested functions for state management, and we see memory usage growing. Discuss how execution context and the scope chain can contribute to memory leaks, and what design changes you’d recommend.

  3. 3

    When bundling modules with a tool like Webpack, how does the concept of execution context affect how variables are scoped across modules, and what pitfalls should we watch for at scale?

8+ years experience
  1. 1

    Our company is migrating a legacy monolith to micro‑frontends, each loaded dynamically. How would you design the loading strategy to ensure each micro‑frontend gets its own execution context and does not interfere with others' scopes, while still allowing shared utilities?

  2. 2

    We need to support user‑provided scripts in a SaaS platform. Describe an architecture that leverages execution context isolation (e.g., VM, iframe) to enforce scope boundaries and protect the host system.

  3. 3

    When designing a server‑side rendering pipeline that runs untrusted JavaScript, how would you separate execution contexts to guarantee that one request's code cannot affect another's scope or global state?

Follow-up Questions

  • Can you walk me through a simple closure that shows how scope and execution context work together?
  • What changes in the execution context cause a variable that was previously accessible to become undefined?
  • How does the value of 'this' relate to the current execution context versus the lexical scope?