Global Scope



examples/javascript/global_scope.js
name = 'Foo'
console.log(name);    // Foo

function f() {
   console.log(name); // Foo
   name = 'Bar'
   console.log(name); // Bar
}
f();

console.log(name);    // Bar

Having var in the body of the code, in the global scope does not matter. It is the same variable all over, but having it inside a function will restrict the scope of that variable.


examples/javascript/global_scope_var.js
var name = 'Foo'
console.log(name);    // Foo

function f() {
   console.log(name); // Foo
   name = 'Bar'
   console.log(name); // Bar
}
f()

console.log(name);    // Bar