01 How JavaScript works & Execution context ?

πŸ”₯ 01. How JavaScript Works – Overview

JavaScript is:

  • Single-threaded β†’ one task at a time

  • Synchronous by default (but supports async with Web APIs)

  • Interpreted/Just-in-time compiled (V8 engine etc.)

JavaScript runs inside the JavaScript Engine (e.g., V8 in Chrome, SpiderMonkey in Firefox). It consists of:

  • Call Stack

  • Memory Heap

  • Execution Context

  • Event Loop


βš™οΈ JavaScript Engine Workflow

  1. Parsing Phase (Compile Time)

    • Code is parsed into tokens

    • Converted into Abstract Syntax Tree (AST)

    • Variables and function declarations are hoisted

  2. Execution Phase

    • JS runs top-to-bottom

    • Creates Execution Contexts for each scope

    • Handles function calls using the Call Stack


🧠 Execution Context (EC) – Core Concept

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

There are 3 main types:

  1. Global Execution Context (GEC)

  2. Function Execution Context (FEC)

  3. Eval Execution Context (rarely used)❌


πŸ”Ή Global Execution Context

Created when the JS file is loaded:

  • Creates a global object (window in browsers, global in Node.js)

  • Binds this to global object

  • Hoists global variables and functions

Example:

var a = 10;

function greet() {
  console.log("Hello");
}

β†’ GEC is created, where:

  • a is added to memory

  • greet function is hoisted


πŸ”Ή Function Execution Context

Created every time a function is invoked:

  • Has its own Variable Environment (VE)

  • Arguments, local variables, inner functions live here

  • Forms part of the Call Stack

Example:

function add(x, y) {
  var total = x + y;
  return total;
}

add(3, 4);

When add(3, 4) is called:

  1. New FEC is created

  2. Parameters x=3, y=4, local total=7

  3. FEC is popped off after return


πŸ“¦ What’s Inside an Execution Context?

Every EC contains:

  1. Variable Environment (VE)

    • var, function declarations

    • arguments object

  2. Lexical Environment

    • Tracks where variables/functions were defined

    • Helps with scope chain

  3. this Binding

    • Depends on how a function is called

πŸ” Execution Stack (Call Stack)

The Call Stack manages Execution Contexts in a LIFO manner:

function one() {
  two();
}
function two() {
  console.log("2");
}
one();

Call Stack Flow:

Global EC
β†’ one() EC
  β†’ two() EC
    β†’ console.log() EC

Each function creates a new EC which is pushed onto the stack, then popped off when done.


🧠 Summary

TermMeaning
Execution ContextThe environment where code is run
Global ECCreated once when script loads
Function ECCreated every time a function runs
Call StackTracks active function calls
Variable EnvironmentStores function variables & arguments

πŸ” Visual Mind Map

JavaScript Engine
β”œβ”€ Memory Heap
β”œβ”€ Call Stack
β”‚  β”œβ”€ GEC
β”‚  β”œβ”€ FEC
β”‚  └─ Eval EC (rare)
└─ Event Loop (later topic)
0
Subscribe to my newsletter

Read articles from Kamlesh Choudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kamlesh Choudhary
Kamlesh Choudhary