Event Loop
Explanation
JavaScript is single-threaded, meaning it executes one task at a time in its call stack. The Event Loop is
the mechanism that allows JavaScript to handle asynchronous operations without blocking the main
thread. Async operations are sent to Web APIs, and once done, their callbacks are queued. The Event
Loop checks if the call stack is empty and runs microtasks (Promises) before macrotasks (setTimeout,
DOM events).
Quick Code Example
[Link]("Start"); setTimeout(() => [Link]("Timeout"), 0); [Link]().then(()
=> [Link]("Promise")); [Link]("End");
Output
Start End Promise Timeout
Key Points
JavaScript is single-threaded
Web APIs handle async work
Microtasks (Promises) run before macrotasks (setTimeout)
Event Loop manages execution order
30-sec Interview Script
The Event Loop lets JavaScript handle async tasks efficiently. Async work goes to Web APIs, then
callbacks go into queues. Microtasks run before macrotasks, ensuring smooth execution.