Javascript - Global vs Local Scope
Code Life
By Pierre Marsaa
Where a Variable Lives
Imagine writing a function that rolls a die during a Codémon battle. Inside that function, you create a variable result to store the roll.
Because result is declared inside the function, it only exists while that function is running. If you try to read result outside the function, JavaScript throws an error:
↗️
Loading editor...
↶
↷
The region of code where a variable can be accessed is called its scope. A variable declared inside a function has local scope (or function scope): it is invisible to the rest of the program.
Verify