02 How JavaScript code is Executed & Call Stack


πŸš€ JavaScript Code Execution Process

JavaScript is a single-threaded, synchronous, non-blocking language that uses an event-driven model with a call stack, heap, Web APIs, task queue, and event loop.


final_execution_context.jpg

πŸ” Step-by-Step: How JS Executes Code

  1. Parsing & Compilation Phase

    • Code is parsed by the JS engine (e.g., V8 in Chrome).

    • Function and variable declarations are hoisted.

    • Scope and memory are set up.

  2. Execution Phase

    • Code runs top to bottom, line by line.

    • Functions are pushed/popped from the call stack when invoked.

    • Variables are resolved using the scope chain.


πŸ“š Components of JavaScript Runtime

1. Call Stack

A LIFO (Last In, First Out) structure that tracks function calls.

function a() {
  b();
}

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

a();

πŸ” Call Stack Flow:

Global()
β†’ a()
   β†’ b()
      β†’ console.log()

Stack Push/Pop:

  • Global() β†’ pushed first

  • a() β†’ pushed

  • b() β†’ pushed

  • console.log() β†’ pushed β†’ popped

  • b() β†’ popped

  • a() β†’ popped

  • Global() β†’ popped


2. Memory Heap

  • Where all objects, arrays, and functions are stored.

  • Unstructured region for dynamic memory allocation.


3. Web APIs (Browser Environment)

JS has access to browser-provided APIs like:

  • setTimeout

  • DOM Events

  • Fetch/XHR

    These run outside the JS engine.


4. Callback / Task Queue

After a Web API (like setTimeout) finishes, it sends the callback to the task queue, waiting for the call stack to be empty.


5. Event Loop

  • Continuously checks if the call stack is empty.

  • If it is, it moves the first task from the queue to the stack.


⏱️ Example with setTimeout

console.log("1");

setTimeout(() => {
  console.log("2");
}, 0);

console.log("3");

Output:

1
3
2

πŸ”„ What Happens:

  1. console.log("1") β†’ Call Stack β†’ logs 1

  2. setTimeout β†’ Web API β†’ after 0ms β†’ pushes callback to queue

  3. console.log("3") β†’ Call Stack β†’ logs 3

  4. Call Stack is now empty β†’ Event Loop moves callback β†’ Call Stack β†’ logs 2


🧠 Key Concepts

ConceptDescription
Call StackTracks function calls
Memory HeapStores variables and objects
Web APIsBrowser features (timers, DOM, etc.)
Callback QueueStores async tasks waiting to run
Event LoopMoves tasks from queue to stack when ready

🎯 Visual Summary

  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Call Stack  β”‚        β”‚ Task Queue β”‚
  β””β”€β”€β”€β”€β–²β”€β”€β”€β”¬β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”¬β”€β”€β”€β–²β”€β”€β”€β”€β”˜
       β”‚   β”‚                   β”‚   β”‚
       β”‚   └─ console.log()    β”‚   └─ (After async complete)
       β”‚                       β”‚
       β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
       └──▢│ Web APIs     β”‚β”€β”€β”€β”€β”˜
           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

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