Event Loop
In one line
The event loop is the mechanism that lets JavaScript perform non-blocking async operations despite being single-threaded.
In simple words
JavaScript runs on a single thread — it can only do one thing at a time. The event loop is what makes async programming possible: it lets JavaScript start an async operation, continue running other code, and come back to handle the result when it is ready.
The event loop has a call stack (where code runs), a microtask queue (Promises), and a macrotask queue (setTimeout, I/O). When the stack is empty, it processes all microtasks, then one macrotask, then all microtasks again, repeating.
This is why setTimeout callbacks run after synchronous code, and why Promise callbacks (microtasks) run before setTimeout callbacks (macrotasks) even if the setTimeout was registered first. Understanding the event loop is key to understanding async JavaScript behavior.
Example
console.log("1");
setTimeout(() => console.log("3"), 0);
Promise.resolve().then(() => console.log("2"));
console.log("1.5");
// Output: 1, 1.5, 2, 3Sync code runs first, then microtasks (Promise), then macrotasks (setTimeout).
Related terms
Related Resources
Related Lessons
- Async JavaScript
Learn about the event loop.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.