DEV Community

Cover image for Beginnerโ€™s Guide #6: ๐ŸŒ€ The Event Loop Explained in 3 Minutes (Youโ€™ll Never Forget It)
Sylwia Laskowska
Sylwia Laskowska

Posted on

Beginnerโ€™s Guide #6: ๐ŸŒ€ The Event Loop Explained in 3 Minutes (Youโ€™ll Never Forget It)

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");
๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung

Output:

๐Ÿš— Car 1
๐Ÿš— Car 2
๐Ÿš— Car 3
๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung

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");
๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung

Output:

โ›ฝ๏ธ Refueling...
๐Ÿš— Another car passing by
โœ… Refuel done!
๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung

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");
๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung

Output:

1๏ธโƒฃ Start
2๏ธโƒฃ End
3๏ธโƒฃ Promise done
4๏ธโƒฃ Timeout done
๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung

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
๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung

โœ… 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:

๐Ÿ‘‰ sylwialask.substack.com

(Yes, I write about real bugs. Yes, most of them were my fault. You're welcome. ๐Ÿ˜…)

Top comments (8)

๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung
 
htho profile image
Hauke T.

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.

๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung
 
sylwia-lask profile image
Sylwia Laskowska

Just WOW!!! Thanks for sharing, her images are incredible! ๐Ÿ’–

๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung
 
lotfijb profile image
Lotfi Jebali

Straight to the point, thanks for sharing

๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung
 
sylwia-lask profile image
Sylwia Laskowska

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! ๐Ÿ™Œ

๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung
 
shemith_mohanan_6361bb8a2 profile image
shemith mohanan

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! ๐Ÿ™Œ

๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung
 
sylwia-lask profile image
Sylwia Laskowska

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.

๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung
 
shreelaxmihegde profile image
Shreelaxmi Hegde

Learnt a new thing today!๐Ÿ™Œ
Thanks for sharing ๐Ÿ˜Š

๐Ÿ“บ Info Siaran: sylwia lask/beginners guide the event loop explained in minutes youll never forget it mk - Jadwal Siaran Langsung
 
sylwia-lask profile image
Sylwia Laskowska

Yay, love to hear that! ๐Ÿ™Œ
You're very welcome โ€” always happy when a post turns into an โ€œaha!โ€ moment for someone ๐Ÿ˜„