0
0
Javascriptprogramming~10 mins

JavaScript execution flow - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - JavaScript execution flow
Start Execution
Parse Code
Create Execution Context
Run Global Code
Function Calls?
YesCreate Function Context
Run Function Code
Return Value
End Execution
JavaScript starts by parsing code, creating an execution context, running global code, then handling function calls by creating new contexts and running their code.
Execution Sample
Javascript
console.log('Start');
function greet() {
  console.log('Hello');
}
greet();
console.log('End');
This code logs messages showing the order JavaScript runs global code and function calls.
Execution Table
StepActionCode LineOutputNotes
1Parse and create global execution contextAll linesPrepare to run code
2Run global code: log 'Start'console.log('Start');StartFirst output
3Define function greetfunction greet() {...}Function stored, not run yet
4Call greet()greet();Create function execution context
5Run greet function code: log 'Hello'console.log('Hello');HelloInside function
6Return from greetEnd of greetFunction ends
7Run global code: log 'End'console.log('End');EndLast output
8End of scriptExecution complete
💡 All code lines executed, no more statements to run
Variable Tracker
Variable/ContextStartAfter Step 3After Step 4After Step 6Final
Global Execution ContextCreatedFunction greet definedFunction greet calledFunction greet returnedExecution finished
Function greet ContextNot createdNot createdCreatedCode run and returnedDestroyed
Key Moments - 3 Insights
Why doesn't the function greet run when it is defined?
Because defining a function only stores it in memory (Step 3), it runs only when called (Step 4).
What happens when greet() is called?
A new function execution context is created (Step 4), the function code runs (Step 5), then it returns (Step 6).
Why does the global code continue after the function call?
Because after the function finishes (Step 6), JavaScript returns to the global context and continues running remaining code (Step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at Step 2?
AHello
BStart
CEnd
DNo output
💡 Hint
Check the 'Output' column at Step 2 in the execution_table
At which step is the function greet actually run?
AStep 3
BStep 4
CStep 5
DStep 7
💡 Hint
Look for when 'Hello' is logged in the output column
If we remove the greet() call, what happens to the output at Step 5?
ANo output at Step 5
BLogs 'Start' again
CIt logs 'Hello' anyway
DLogs 'End' earlier
💡 Hint
Without calling greet(), the function code does not run (see Step 4 and 5)
Concept Snapshot
JavaScript execution flow:
- Parse code and create global context
- Run global code line by line
- Define functions but don't run them yet
- When function called, create new context and run it
- After function returns, continue global code
- Execution ends when no code left
Full Transcript
JavaScript starts by reading and parsing the entire code, then creates a global execution context. It runs the global code line by line. When it sees a function definition, it stores the function but does not run it immediately. When the function is called, JavaScript creates a new execution context for that function and runs its code. After the function finishes, it returns to the global context and continues running the remaining code. Execution ends when all code has been run.