The scope of a given variable can be thought of as the areas within the program where that variable can be accessed.
When we declare a variable at the beginning of a module (outside all functions), we think that it's only natural that this variable should then be accessible to all functions within the module:
var hello = 'hi';
function a() {
hello; // a() can "see" the hello variable
}
function b() {
hello; // b() can "see" the hello variable
}
And if we define a variable within a function, then we expect all inner functions to have access to it:
var value = 'I exist';
function doSomething() {
value; // => "I exist"
}
The fact that we can access value in the doSomething function here is thanks to its scope. The scope of a given variable will depend on how it is declared. When you declare a variable...