What is the event loop in JavaScript?
Understand how the JavaScript event loop handles asynchronous operations and manages concurrency.
Advertisement
Question
What is the event loop in JavaScript?
Answer
The event loop allows JavaScript to perform non-blocking operations, even though it runs on a single thread.
It manages:
- Call Stack
- Callback Queue
- Microtask Queue
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
Promise.resolve().then(() => console.log('Promise'));
console.log('End');
Output:
Start
End
Promise
Timeout
Promises (microtasks) run before callbacks (macrotasks).
Real-World Example
Event loops power async rendering, timers, and server I/O in Node.js.
Quick Practice
Predict the output of a snippet mixing setTimeout and Promise.resolve().
Summary
The event loop orchestrates async execution — the heartbeat of JavaScript’s runtime.
Does JavaScript run multiple threads?
No. It’s single-threaded but uses asynchronous callbacks managed by the event loop.
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement