0
0
Javascriptprogramming~10 mins

Global execution context in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Global execution context
Start: Script loads
Create Global Execution Context
Memory Creation Phase
Variable & Function Declarations Stored
Code Execution Phase
Execute Code Line by Line
Global Context Remains Active Until Script Ends
When a JavaScript file runs, the global execution context is created first, setting up memory for variables and functions, then runs the code line by line.
Execution Sample
Javascript
var a = 5;
function greet() {
  console.log('Hello');
}
greet();
This code creates a global variable and a function, then calls the function to print 'Hello'.
Execution Table
StepActionMemory StateOutput
1Create global execution contextEmpty memory for variables and functions
2Memory creation phase: store 'a' as undefined, store 'greet' functiona: undefined, greet: function
3Code execution: assign 5 to 'a'a: 5, greet: function
4Code execution: call greet()a: 5, greet: functionHello
5End of scripta: 5, greet: function
💡 Script ends, global execution context is removed
Variable Tracker
VariableStartAfter Step 2After Step 3Final
aundefinedundefined55
greetundefinedfunctionfunctionfunction
Key Moments - 2 Insights
Why is variable 'a' initially undefined in memory before assignment?
During the memory creation phase (see step 2 in execution_table), variables are set to undefined before code runs. This is why 'a' is undefined before step 3 assigns 5.
Why can the function 'greet' be called before its code line?
Functions are fully stored in memory during the creation phase (step 2), so they can be called anytime after that, even before their code line appears.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'a'?
Aundefined
B5
Cfunction
Dnull
💡 Hint
Check the 'Memory State' column at step 3 in the execution_table
At which step is the function 'greet' stored in memory?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Memory State' column in execution_table rows to see when 'greet' appears
If we remove the function call 'greet()', what will be the output?
ANo output
Bundefined
CHello
DError
💡 Hint
Check the 'Output' column in execution_table, see when 'Hello' is printed
Concept Snapshot
Global Execution Context in JavaScript:
- Created when script starts
- Memory phase: variables set to undefined, functions stored
- Execution phase: code runs line by line
- Functions can be called anytime after memory phase
- Context lasts until script ends
Full Transcript
When JavaScript runs a script, it first creates the global execution context. This sets up memory for variables and functions. Variables start as undefined, and functions are stored fully. Then the code runs line by line, assigning values and calling functions. For example, variable 'a' starts undefined, then gets value 5. The function 'greet' is stored early and can be called anytime after. The output 'Hello' appears when greet() runs. The global context stays active until the script finishes.