Skip to content

Instantly share code, notes, and snippets.

@iwek
Last active December 11, 2015 10:28
Show Gist options
  • Save iwek/4587195 to your computer and use it in GitHub Desktop.
Save iwek/4587195 to your computer and use it in GitHub Desktop.
JavaScript Variables and Scope
v1 = 1; // Global Scope
var v2 = 2; // Variable defined with var keyword but not within a function: Global Scope
function f() {
v3 = 3; // No var keyword: Global Scope
var v4 = 4; // Local Scope only
var v5 = v1+v2+v3; //1+2+3=6 - Local Scope only, can access v1 and v2 variables from the Global Scope
}
v1; //1 - JavaScript can access the variable as it lives in the Global Scope
v2; //2 - JavaScript can access the variable as it lives in the Global Scope
v3; //3 - JavaScript can access the variable as it lives in the Global Scope
v4; //ReferenceError: v4 is not defined (JavaScript cannot access the local scope of function f()
v5; //ReferenceError: v5 is not defined (JavaScript cannot access the local scope of function f()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment