Skip to content

Instantly share code, notes, and snippets.

@turtlemonvh
Created August 7, 2013 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turtlemonvh/6178700 to your computer and use it in GitHub Desktop.
Save turtlemonvh/6178700 to your computer and use it in GitHub Desktop.
For loop scope test in javascript
for(var i=1; i<10; i++){var x = 3;};
x
@turtlemonvh
Copy link
Author

Yeah, javascript doesn't have block scope.

If you're iterating with .Each, you can get function scope. For example:

var a = [1,2,3,4,5]; a.forEach(function(v){ console.log(v); }); try{v}catch(e){console.log('v is not defined anymore')}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment