JavaScript Internals: Call Stack, Web APIs, and Event Loop

🚀 How JavaScript Runs Internally While revising JavaScript internals, I went through how JavaScript code actually executes step by step. 👉 JavaScript code cannot run without being pushed to the Call Stack. Execution always starts with the creation of the Global Execution Context (GEC), which is pushed to the call stack. 🔹 Global Execution Context GEC has two phases: Memory Phase – variables are allocated memory and initialized Code Phase – code executes line by line 🔹 Web APIs (setTimeout, events, promises) When setTimeout is used: It is not part of JavaScript It runs inside Web APIs The timer starts in Web APIs The Call Stack never waits; remaining synchronous code executes immediately Once the timer completes: The callback is pushed to the Task / Callback Queue 🔹 Promises & Microtask Queue Promises also go to Web APIs After resolving, they move to the Microtask Queue Microtask Queue has higher priority than Task Queue 🔹 Event Loop The Event Loop continuously monitors: Whether the Call Stack is empty Whether Microtask or Task Queues have pending callbacks If the Call Stack is empty: Microtask Queue callbacks are pushed first Then Task Queue callbacks are pushed 🔹 Starvation When there is heavy or infinite promise chaining, microtasks keep getting executed and task queue callbacks may never get a chance to enter the call stack. This situation is called Starvation. #JavaScript #EventLoop #CallStack #AsyncJS #InterviewPreparation

To view or add a comment, sign in

Explore content categories