Event Loop in JavaScript

From the Async JavaScript cheat sheet ยท Timers & Event Loop ยท verified Jul 2026

Event Loop

Understand how JavaScript handles asynchronous operations through the event loop mechanism

javascript
// Event loop phases
console.log('1'); // Call stack

setTimeout(() => console.log('2'), 0); // Macrotask queue

Promise.resolve()
  .then(() => console.log('3')); // Microtask queue

console.log('4'); // Call stack

// Output: 1, 4, 3, 2
๐Ÿ’ก JavaScript is single-threaded but uses an event loop for async operations
โšก Microtasks (promises) have priority over macrotasks (setTimeout)
๐Ÿ“Œ Understanding the event loop helps debug timing issues
๐Ÿ”„ Call stack must be empty before event loop processes callbacks

More JavaScript tasks

Back to the full Async JavaScript cheat sheet