0
0
Javascriptprogramming~10 mins

Function scope in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function scope
Enter function
Create local variables
Execute function body
Access local variables inside function
Exit function
Local variables destroyed
Try to access local variables outside? --> ReferenceError
When a function runs, it creates its own space for variables. These variables only exist inside the function and disappear when it ends.
Execution Sample
Javascript
function greet() {
  let message = 'Hello';
  console.log(message);
}
greet();
console.log(message);
This code shows a variable inside a function that cannot be used outside.
Execution Table
StepActionVariable 'message'OutputNotes
1Function greet() is definedundefinedNo variables created yet
2Call greet()undefinedEnter function scope
3Create local variable message = 'Hello''Hello'message exists only inside greet
4console.log(message)'Hello'HelloPrints 'Hello' inside function
5Function greet() endsundefinedLocal variable destroyed
6console.log(message) outsideundefinedReferenceErrormessage not defined outside function
💡 Function ends and local variables are removed; accessing them outside causes ReferenceError
Variable Tracker
VariableStartAfter Step 3After Step 5Final
messageundefined'Hello'undefinedundefined
Key Moments - 2 Insights
Why can't we use the variable 'message' outside the function?
Because 'message' is created inside the function and only exists there. After the function ends (see step 5), it is removed, so outside the function (step 6) it causes a ReferenceError.
Does the variable 'message' exist before the function is called?
No, 'message' is created only when the function runs (step 3). Before that, it is undefined.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'message' after step 3?
AReferenceError
B'Hello'
Cundefined
Dnull
💡 Hint
Check the 'Variable message' column at step 3 in the execution table.
At which step does the function 'greet' end and local variables get destroyed?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Action' and 'Notes' columns for when the function ends.
If we move 'console.log(message)' inside the function after step 3, what will happen?
AIt will print 'Hello'
BIt will cause ReferenceError
CIt will print undefined
DIt will print null
💡 Hint
See step 4 where console.log(message) inside the function prints 'Hello'.
Concept Snapshot
Function scope means variables declared inside a function exist only there.
They are created when the function runs and destroyed when it ends.
Trying to access them outside causes a ReferenceError.
Use console.log inside the function to see local variables.
Variables outside functions are global and accessible everywhere.
Full Transcript
This visual shows how function scope works in JavaScript. When the function greet() is called, it creates a local variable message with value 'Hello'. This variable exists only inside the function. The console.log inside the function prints 'Hello'. After the function ends, the variable message is destroyed. Trying to access message outside the function causes a ReferenceError because it does not exist there. This shows that variables inside functions are private to that function and cannot be used outside.