Last active
December 11, 2015 10:28
-
-
Save iwek/4587195 to your computer and use it in GitHub Desktop.
JavaScript Variables and Scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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