Created
February 25, 2014 15:11
-
-
Save Xartok/9210708 to your computer and use it in GitHub Desktop.
Javascript doesn't have block scope
This file contains hidden or 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
| var f = (function() { | |
| for(var i = 1; i <= 3; i++){ | |
| var j = i + 1; // with block scope, j would be inaccesible outside the for statement | |
| } | |
| return j; // however, j is accessible here, because Javascript doesn't have block scope | |
| })(); | |
| console.log("f: ", f); // as a result, f value is 4, not undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment