0
0
Javascriptprogramming~10 mins

JavaScript runtime overview - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - JavaScript runtime overview
Start JS Program
Parse Code
Create Execution Context
Run Global Code
Call Functions
Manage Call Stack
Handle Async Tasks
Event Loop Checks
Execute Callback Queue
Program Ends
JavaScript runtime starts by parsing code, creating an execution context, running code, managing function calls with a call stack, handling asynchronous tasks via event loop and callback queue, then ends.
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 main code finishes.
Execution Table
StepActionCall StackCallback QueueOutput
1Parse and start global codeGlobal Execution ContextEmpty
2Execute console.log('Start')Global Execution ContextEmptyStart
3Call setTimeout with callbackGlobal Execution ContextTimeout callback scheduledStart
4Execute console.log('End')Global Execution ContextTimeout callback scheduledStart End
5Global code finished, call stack emptyEmptyTimeout callback scheduledStart End
6Event loop moves callback to call stackTimeout callbackEmptyStart End
7Execute callback console.log('Timeout')Timeout callbackEmptyStart End Timeout
8Callback finished, call stack emptyEmptyEmptyStart End Timeout
💡 Program ends when call stack and callback queue are empty
Variable Tracker
VariableStartAfter Step 3After Step 5Final
Call StackGlobal Execution ContextGlobal Execution ContextEmptyEmpty
Callback QueueEmptyTimeout callback scheduledTimeout callback scheduledEmpty
OutputStartStart EndStart End Timeout
Key Moments - 2 Insights
Why does 'Timeout' print after 'End' even though setTimeout delay is 0?
Because setTimeout callback goes to the callback queue and waits until the call stack is empty (see execution_table steps 5-7). The event loop ensures callbacks run only after main code finishes.
What is the call stack and why does it matter?
The call stack tracks which function is running. JavaScript runs one thing at a time. When the stack is empty, async callbacks can run (see execution_table steps 3, 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is in the call stack at step 4?
AGlobal Execution Context
BTimeout callback
CEmpty
DCallback Queue
💡 Hint
Check the 'Call Stack' column at step 4 in the execution_table
At which step does the callback queue become empty?
AStep 3
BStep 6
CStep 8
DStep 7
💡 Hint
Look at the 'Callback Queue' column in the execution_table to see when it becomes empty
If we remove setTimeout, how would the output change?
AOutput would be 'Timeout' only
BOutput would be empty
COutput would be 'Start End' only
DOutput would be 'Start Timeout End'
💡 Hint
Check the output column in execution_table steps 2 and 4 to see what prints without setTimeout
Concept Snapshot
JavaScript runtime parses code and creates an execution context.
It runs code using a call stack (one thing at a time).
Async tasks go to callback queue.
Event loop moves callbacks to call stack when empty.
This ensures non-blocking behavior.
Full Transcript
JavaScript runtime works by first parsing your code and creating an execution context where your code runs. It uses a call stack to keep track of what function is running. When you run code like console.log, it goes on the call stack and runs immediately. When you use asynchronous functions like setTimeout, the callback is placed in a callback queue. The event loop watches the call stack and when it is empty, it moves callbacks from the queue to the stack to run them. This is why even a setTimeout with zero delay runs after the main code finishes. This system lets JavaScript run code without waiting and keeps your program responsive.