← Back to Blog

🔁 JavaScript Event Loop - Explained Simply

BEGINNER
Ever wonder how JS handles async stuff like setTimeout without freezing the page? It’s all thanks to the Event Loop. Here's how it works in 4 parts: 1- Call Stack 2- Web APIs 3- Callback Queue 4- Event Loop (the boss 😎) 💾 Save this if you’re learning JavaScript
JavaScript Event Loop - Explained Simply — slide 1/8
JavaScript Event Loop - Explained Simply — slide 2/8
JavaScript Event Loop - Explained Simply — slide 3/8
JavaScript Event Loop - Explained Simply — slide 4/8
JavaScript Event Loop - Explained Simply — slide 5/8
JavaScript Event Loop - Explained Simply — slide 6/8
JavaScript Event Loop - Explained Simply — slide 7/8
JavaScript Event Loop - Explained Simply — slide 8/8

Ever wonder how JavaScript handles async work like setTimeout without freezing the page? It's all thanks to the event loop, which coordinates four parts: the call stack, the Web APIs, the callback queue, and the loop itself.

Example

console.log("Start");

setTimeout(() => {
  console.log("Inside setTimeout");
}, 0);

console.log("End");

Output:

Start
End
Inside setTimeout

Why? Because setTimeout is handled by the Web API, then sent to the queue, and only runs after the call stack is clear — even with a delay of 0.

1. Call stack

Where functions run, one at a time. It is LIFO (last in, first out): each call pushes a function execution context on top of the global execution context, and it is popped off when the function returns.

2. Web APIs / browser APIs

Timers, DOM events, and fetch or XMLHttpRequest live here. They are handled by the browser, not by the call stack.

3. Callback queue / task queue

Holds callbacks — like onClick or onLoad handlers — waiting to be executed after the stack is clear.

4. Event loop (the boss)

The event loop constantly checks whether the call stack is empty. If it is, it pushes the next callback from the queue onto the stack to be executed.