⚙️ JavaScript Engine, Execution & Scope
Estimated reading: 4 minutes 10 views

🧠 JavaScript – Execution Context: Deep Dive into Code Execution


🧲 Introduction – What Happens When JavaScript Runs?

Have you ever wondered how JavaScript knows which line of code to execute and in what order? 🧐
That’s where the Execution Context comes into play!

Understanding JavaScript Execution Context is crucial for mastering concepts like:

  • 🌀 Scope and closures
  • 🧮 Hoisting
  • 🔄 The call stack
  • 🧵 Asynchronous code behavior

By the end of this guide, you’ll grasp how JavaScript processes your code under the hood, from parsing to running line-by-line.


🔑 What is an Execution Context?

An Execution Context is an environment in which JavaScript code is evaluated and executed.

Each time a piece of code runs—whether globally, inside a function, or via eval()a new execution context is created.

🧱 Components of an Execution Context:

  1. Variable Environment (VE):
    Stores variables and function declarations.
  2. Lexical Environment (LE):
    Holds the identifier-resolution logic and closures.
  3. This Binding:
    Defines the value of this depending on how the function is called.

📘 Note: In modern engines, VE and LE are often combined into a Lexical Environment Record for optimization.


📚 Types of Execution Contexts in JavaScript

Execution ContextDescription
Global Execution Context (GEC)Default context where code starts executing. Only one exists.
Function Execution Context (FEC)Created every time a function is invoked. Multiple instances can exist simultaneously.
Eval Execution ContextRarely used. Created when code is executed inside eval() (discouraged).

🧪 Global Execution Context (GEC) Explained

When a JS file runs, the Global Execution Context is created first.

let a = 10;
function greet() {
  console.log("Hello!");
}

What happens internally:

  1. Creation Phase
    • Memory is allocated for a and greet()
    • a = undefined
    • greet = function definition
  2. Execution Phase
    • a is assigned 10
    • greet() can now be invoked

📘 Note: This is where hoisting comes in — variables and function declarations are moved to the top of their scope.


🔁 Function Execution Context (FEC)

Each time a function is called, a new Function Execution Context is created and pushed to the call stack.

function sum(x, y) {
  let result = x + y;
  return result;
}

let total = sum(5, 3);

Steps involved:

  1. Create Phase
    • Allocate memory for x, y, and result
    • All initialized as undefined
  2. Execution Phase
    • x = 5, y = 3
    • result = 8
    • return 8

📘 Call Stack Flow:

  • GEC is created
  • sum() is called → FEC is created and pushed
  • On return, FEC is popped
  • GEC resumes

🧠 Execution Context Lifecycle

🔄 1. Creation Phase

  • Scope chain is formed
  • Variables, functions, and arguments are hoisted

🏃 2. Execution Phase

  • Assigns values to variables
  • Executes code line-by-line
  • Handles return statements

🧮 Execution Stack (Call Stack) and Context Order

The Call Stack (a.k.a Execution Stack) keeps track of execution contexts.

function first() {
  second();
  console.log("First");
}

function second() {
  console.log("Second");
}

first();

Call Stack Steps:

  1. Push GEC
  2. Call first() → Push FEC: first()
  3. Inside first() → Call second() → Push FEC: second()
  4. Log “Second” → Pop second()
  5. Log “First” → Pop first()
  6. End → Pop GEC

🧵 Execution Context vs Scope

FeatureExecution ContextScope
LifecycleCreated and destroyed as code runsExists statically in code
DefinesHow and when code is runWhere variables are accessible
MemoryManages memory allocation and reference creationLexical environment chain

⚠️ Common Mistakes with Execution Context

Confusing hoisting with initialization

    console.log(a); // undefined, not ReferenceError
    var a = 5;

    Assuming this always points to the same object

      function foo() {
        console.log(this);
      }
      foo(); // In non-strict mode: Window / global

      Expecting function-level scope for let/const

        {
          let a = 10;
        }
        console.log(a); // ReferenceErro

        💡 Tips for Mastering Execution Contexts

        • ✅ Visualize with call stack diagrams
        • ✅ Practice with debugger and browser DevTools
        • ✅ Understand hoisting and variable lifecycles
        • ✅ Embrace block scoping (let, const) over var

        🧾 Summary – What You’ve Learned

        • JavaScript creates a new execution context every time code runs
        • Global, function, and eval contexts handle different code blocks
        • Contexts are managed in a call stack, allowing JS to handle nested calls
        • Understanding context helps with scope, hoisting, and this binding

        ❓ Frequently Asked Questions (FAQ)

        What is the difference between Execution Context and Call Stack?
        The Execution Context is the environment in which code executes, while the Call Stack is the data structure that tracks the order of execution contexts.

        When is a new Execution Context created?
        Every time JavaScript runs code in the global scope or invokes a function, it creates a new Execution Context.

        Does each function have its own Execution Context?
        Yes. Each function call creates a new execution context with its own variables, this, and scope chain.

        How does JavaScript manage multiple Execution Contexts?
        JavaScript uses a Call Stack to push and pop execution contexts as functions are called and return.

        What is hoisting in the context of Execution Context?
        During the creation phase, variable and function declarations are hoisted—i.e., moved to the top of their scope before code execution.


        Share Now :

        Leave a Reply

        Your email address will not be published. Required fields are marked *

        Share

        JavaScript — Execution Context

        Or Copy Link

        CONTENTS
        Scroll to Top