Ever wondered why your code sometimes runs in a different order than you expected?
Like:
โWhy did this run before that?!โ ๐ตโ๐ซ
Congratulations โ you just met one of the most important parts of JavaScript:
โจ The Event Loop
Letโs break it down super simply. No computer science degree required. ๐ง ๐ฌ
๐ฆ JavaScript Is a One-Lane Road
JavaScript runs on one main thread โ imagine a tiny road with one single lane.
Only one car (piece of code) can drive at a time. No overtaking. No shortcuts.
console.log("๐ Car 1");
console.log("๐ Car 2");
console.log("๐ Car 3");
Output:
๐ Car 1
๐ Car 2
๐ Car 3
Easy. One after another.
๐ฌ But What If One Car Stops?
What if a car pulls over to refuel โฝ๏ธ?
If it just stopped on the road, everything behind it would be stuck.
JavaScript says:
โGo refuel somewhere else โ Iโll keep the road free.โ โ
Thatโs asynchronous (async) code.
console.log("โฝ๏ธ Refueling...");
setTimeout(() => {
console.log("โ
Refuel done!");
}, 2000);
console.log("๐ Another car passing by");
Output:
โฝ๏ธ Refueling...
๐ Another car passing by
โ
Refuel done!
Even though setTimeout appears first, it finishes later โ because JavaScript moves it off the main road, so the rest of the code can keep running.
๐ Soโฆ What Is the Event Loop?
Hereโs what happens behind the scenes:
| Thing | What it does | Emoji |
|---|---|---|
| Call Stack | The main road where JS runs code line by line | ๐ฃ๏ธ |
| Web APIs (browser) / libuv (Node.js) | Parking lot for async work | ๐ ฟ๏ธ |
| Callback Queue | Regular waiting line for async callbacks | ๐ฌ |
| Microtask Queue | VIP fast-lane (for Promises, mutation observers, etc.) | ๐ |
| Event Loop | The traffic officer that decides who enters the road next | ๐ฎ |
๐งช Try This in the Console
console.log("1๏ธโฃ Start");
setTimeout(() => console.log("4๏ธโฃ Timeout done"), 0);
Promise.resolve().then(() => console.log("3๏ธโฃ Promise done"));
console.log("2๏ธโฃ End");
Output:
1๏ธโฃ Start
2๏ธโฃ End
3๏ธโฃ Promise done
4๏ธโฃ Timeout done
Why? ๐ค
-
Promise.then()goes to the microtask queue (high priority โ ) -
setTimeout()goes to the callback queue (lower priority โณ) - The Event Loop always empties the microtask queue first
Even if setTimeout(..., 0) has 0 ms delay, it still waits behind microtasks.
๐ Visual Summary
Your Code โ Call Stack
setTimeout โ Web API / libuv
โ (after delay)
Callback Queue
Promise.then โ Microtask Queue โ VIP priority
Event Loop โ Moves tasks to Call Stack when it's empty
โ
Microtasks always run before callbacks
โ setTimeout(..., 0) never means โrun instantlyโ
๐ก Bonus: What About Web Workers?
So far, we said JavaScript has only one lane (single thread).
But there is a way to get more lanes:
๐ท Web Workers
- Run JavaScript in separate threads โ
- Donโt block the main thread โ
- Cannot access the DOM directly โ
- Communicate with the main thread using
postMessage๐ฉ (no shared memory)
Perfect for:
- Heavy calculations ๐งฎ
- Image processing ๐ผ๏ธ
- Long loops ๐
They donโt change the Event Loop โ theyโre just extra helpers doing work in parallel.
๐ง TL;DR (Like You're 5)
| Concept | Meaning | Emoji |
|---|---|---|
| Call Stack | Main road | ๐ฃ๏ธ |
| Web APIs / libuv | Parking lot for async | ๐ ฟ๏ธ |
| Callback Queue | Cars waiting in line | ๐๐ฌ |
| Microtask Queue | VIP instant lane (Promises) | ๐ |
| Event Loop | Traffic cop | ๐ฎ |
| Web Worker | Extra road (no DOM access) | ๐ฃ๏ธ๐ ๏ธ |
โจ Final Thought
JavaScript isnโt slow โ itโs organized.
It never blocks the road if it doesnโt have to.
The Event Loop makes async feel like magicโฆ until you understand it.
Then it stops being magic and becomes predictable. ๐๐
๐ Hi, I'm Sylwia
I break code so you don't have to. ๐ ๏ธ๐ฅ
If you enjoy laughing at past bugs and learning from them, follow me here or on my Substack:
(Yes, I write about real bugs. Yes, most of them were my fault. You're welcome. ๐ )
Top comments (8)
I liked your post. But my favorite on the basics are the posts by @lydiahallie .
Unfortunately her post on the event loop has degraded (some images are lost).
But the complete version is still on her Blog:
lydiahallie.com/blog/event-loop
These are the types of outstanding posts to give to your juniors.
Just WOW!!! Thanks for sharing, her images are incredible! ๐
Straight to the point, thanks for sharing
Thanks! Glad you liked the straight-to-the-point style ๐
I try to keep the โno fluff, just clarityโ rule โ especially for topics that are usually over-explained.
Appreciate you taking the time to read! ๐
This is probably the clearest Event Loop explanation Iโve read โ love the road and traffic cop analogy! ๐๐ฎโโ๏ธ Makes async behavior finally click in a simple way. Bookmarked for quick refresh later! ๐
Ooo that means a lot โ thank you! ๐
Iโm glad the cars + traffic cop analogy did its job ๐
The Event Loop sounds complicated only until someone explains it like real life instead of a CS textbook.
Learnt a new thing today!๐
Thanks for sharing ๐
Yay, love to hear that! ๐
You're very welcome โ always happy when a post turns into an โaha!โ moment for someone ๐