🧭Event Loop
Also known as JavaScript Event Loop, Run Loop
The loop that picks which waiting job runs next. It keeps one thread busy.
Think of it like
Think of it like a waiter with one pair of hands. The waiter serves one table, then checks who is ready.
Example
Jack logs 1, sets a 0 ms timer for 2, then logs 3. The page prints 1, 3, 2, because the timer waits for the queue.
Why it matters
It explains why a long loop freezes a page. It also shows why code may not run in the order written.
Definition
Example code
javascript
console.log(1);
setTimeout(() => console.log(2), 0);
console.log(3);
// 1
// 3
// 2Common mistake
Key takeaways
- 1One thing at a time. On a single thread.
- 2A queue of jobs. Waiting their turn.
- 3Promises first. Then timers.