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