Understanding JavaScript Event Loop
Learn how JavaScript handles asynchronous operations with the event loop.

How Event Loop Works#
The event loop is what allows JavaScript to perform non-blocking operations. It continuously checks the call stack and callback queue.
Call Stack#
Code
function first() {
console.log('First');
second();
}
function second() {
console.log('Second');
third();
}
function third() {
console.log('Third');
}
first(); // Call stack: first -> second -> thirdAsynchronous Operations#
Code
console.log('Start');
setTimeout(() => {
console.log('Timeout');
}, 0);
Promise.resolve().then(() => {
console.log('Promise');
});
console.log('End');
// Output: Start, End, Promise, TimeoutMicrotasks vs Macrotasks#
- Microtasks: Promises, queueMicrotask
- Macrotasks: setTimeout, setInterval, I/O
- Microtasks execute before macrotasks