Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Global Scope

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.

var name = 'Foo'
console.log(name);    // Foo

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

console.log(name);    // Bar