0
0
Javascriptprogramming~10 mins

What execution context is in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - What execution context is
Start Program
Create Global Execution Context
Execute Global Code
Function Called?
NoEnd
Yes
Create Function Execution Context
Execute Function Code
Function Returns
Destroy Function Execution Context
Back to Previous Context
Loop if more functions called
Execution context is like a container that holds information about the environment where JavaScript code runs, starting with the global context and creating new ones for each function call.
Execution Sample
Javascript
let x = 10;
function add(y) {
  return x + y;
}
let result = add(5);
console.log(result);
This code runs in the global context, then creates a new context when the function add is called.
Execution Table
StepExecution ContextActionVariables in ContextOutput
1GlobalDeclare x=10 and function addx=10, add=function
2GlobalCall add(5)x=10, add=function
3Function addCreate context with y=5y=5
4Function addCalculate x + yx=10 (from outer), y=5
5Function addReturn 15y=5
6GlobalAssign result=15x=10, add=function, result=15
7Globalconsole.log(result)x=10, add=function, result=1515
8GlobalEnd programx=10, add=function, result=15
💡 Program ends after global context finishes executing all code.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 6Final
xundefined10101010
addundefinedfunctionfunctionfunctionfunction
yundefinedundefined5undefinedundefined
resultundefinedundefinedundefined1515
Key Moments - 2 Insights
Why does the function add have access to variable x even though x is not declared inside it?
Because the function execution context can access variables from its outer (global) context, as shown in step 4 where x=10 is used inside the function.
What happens to the function's variable y after the function finishes?
The function execution context is destroyed after returning, so y is no longer accessible in the global context, as seen in variable_tracker where y becomes undefined again after step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 6?
A15
B5
Cundefined
D10
💡 Hint
Check the 'Variables in Context' column at step 6 in the execution_table.
At which step is the function execution context created?
AStep 2
BStep 3
CStep 5
DStep 7
💡 Hint
Look for the step where 'Function add' context is mentioned in the execution_table.
If the function add did not return a value, what would be the output at step 7?
A15
BNaN
Cundefined
DError
💡 Hint
Recall that if no return is given, functions return undefined by default.
Concept Snapshot
Execution context is the environment where JS code runs.
Global context is created first.
Each function call creates a new context.
Contexts hold variables and code info.
Contexts are destroyed after function ends.
Full Transcript
Execution context in JavaScript is like a container that holds all the information needed to run code. When a program starts, the global execution context is created. This context holds global variables and functions. When a function is called, a new execution context is created for that function. This function context has its own variables and parameters but can also access variables from outer contexts. After the function finishes running, its context is destroyed and control returns to the previous context. This process continues until the program ends.