The Event Loop is the mechanism that enables JavaScript's non-blocking, asynchronous behavior despite being single-threaded. Understanding it explains many confusing JavaScript behaviors. Runtime Components The JavaScript runtime has several components: Call Stack — a LIFO stack where all synchronous code runs. Web APIs — browser-provided async capabilities (setTimeout, fetch, DOM events). Callback Queue (Task Queue) — where Web API callbacks wait after completion. Microtask Queue — higher-priority queue for Promise callbacks (.then, .catch) and mutation observers. Event Loop — the process that constantly checks if the Call Stack is empty; if so, it moves tasks from queues onto the stack. Execution Order Execution order: (1) All synchronous code on the Call Stack runs first. (2) All…
Key Takeaways
Call Stack → Microtask Queue → Macrotask Queue.
Promise .then() callbacks are microtasks (higher priority than setTimeout).
Event Loop only runs when the Call Stack is empty.
Blocking the call stack (heavy loops) freezes the entire UI.
Use Web Workers for heavy computation to keep UI responsive.