Think of JavaScript as a single-threaded waiter in a restaurant (the Runtime).
The Players
- Call Stack (The Waiter): Can only do one thing at a time. Taking an order, serving a plate.
- Web APIs (The Kitchen): Where work happens in the background (timers, fetch requests). The waiter hands off the order here and goes back to work.
- Callback Queue (The Pick-up Counter): When the kitchen is done, they put the food (callback) here.
- Event Loop ( The Manager): Continually checks: “Is variable Stack empty? AND Is there something in the Queue?”
The Flow
console.log('Start')-> Waiter serves water immediately.setTimeout(..., 0)-> Waiter hands order to kitchen. Kitchen immediately puts it on the Pick-up Counter.console.log('End')-> Waiter serves bread.- Stack is empty. Manager sees food on counter.
- Waiter serves the
setTimeoutcallback.
This is why setTimeout(..., 0) doesn’t run immediately—it has to wait for the stack to clear!