Skip to content

Instantly share code, notes, and snippets.

@Xartok
Created February 25, 2014 15:11
Show Gist options
  • Select an option

  • Save Xartok/9210708 to your computer and use it in GitHub Desktop.

Select an option

Save Xartok/9210708 to your computer and use it in GitHub Desktop.
Javascript doesn't have block scope
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