0
0
Javascriptprogramming~10 mins

Event loop overview in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Event loop overview
Start: JS code runs
Call stack executes sync code
Async task encountered?
Send async task to Web APIs
Async task completes
Callback added to task queue
Event loop checks call stack
If call stack empty, move callback to call stack
Callback executes
Repeat until all tasks done
JavaScript runs code on a call stack, sends async tasks to Web APIs, and uses the event loop to move callbacks back to the stack when ready.
Execution Sample
Javascript
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
console.log('End');
This code logs 'Start', schedules a timeout callback, then logs 'End'. The timeout callback runs after the sync code.
Execution Table
StepCall StackWeb APIsTask QueueEvent Loop ActionOutput
1console.log('Start')Execute sync codeStart
2console.log('Start') done, removed from stack
3setTimeout(callback, 0)Timer startedSchedule async task
4Timer counting down
5console.log('End')Execute sync codeEnd
6console.log('End') done, removed from stack
7Timer doneCallback readyEvent loop checks stack empty
8callback()Move callback to call stack
9console.log('Timeout')Execute callbackTimeout
10callback done, removed from stack
11All tasks done, program ends
💡 All synchronous code executed and all callbacks processed from the task queue.
Variable Tracker
VariableStartAfter Step 3After Step 7Final
Call StackEmptysetTimeout(callback, 0)callback()Empty
Web APIsEmptyTimer startedTimer doneEmpty
Task QueueEmptyEmptyCallback readyEmpty
OutputStartEndTimeout
Key Moments - 2 Insights
Why does 'Timeout' print after 'End' even though setTimeout delay is 0?
Because setTimeout callback goes to the task queue and waits until the call stack is empty (see steps 5-9 in execution_table).
What happens if the call stack is not empty when the async task finishes?
The event loop waits and does not move the callback to the call stack until it becomes empty (see step 7 and 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is on the call stack at step 3?
Aconsole.log('Start')
BsetTimeout(callback, 0)
Ccallback()
Dconsole.log('End')
💡 Hint
Check the 'Call Stack' column at step 3 in the execution_table.
At which step does the event loop move the callback from the task queue to the call stack?
AStep 8
BStep 5
CStep 7
DStep 9
💡 Hint
Look for 'Move callback to call stack' in the 'Event Loop Action' column.
If the call stack was busy at step 7, when would the callback run?
ABefore the synchronous code finishes
BImmediately at step 7
CAfter the call stack becomes empty
DNever
💡 Hint
Refer to the explanation in key_moments about event loop waiting for empty call stack.
Concept Snapshot
JavaScript runs synchronous code on the call stack.
Async tasks go to Web APIs and then to the task queue.
The event loop moves callbacks to the call stack when it's empty.
This ensures non-blocking, ordered execution of async code.
Full Transcript
JavaScript executes code on a single call stack. When it encounters asynchronous tasks like setTimeout, it sends them to Web APIs to wait. Once the async task finishes, its callback is placed in the task queue. The event loop constantly checks if the call stack is empty. If it is, it moves the callback from the task queue to the call stack to execute it. This process allows JavaScript to handle asynchronous operations without blocking the main thread. In the example, 'Start' and 'End' print first because they are synchronous. The 'Timeout' callback runs last, even with zero delay, because it waits for the call stack to clear.