Skip to content

Instantly share code, notes, and snippets.

@rjmcdonald83
Forked from yanatan16/for_var_scoping.js
Created August 8, 2014 20:32
Show Gist options
  • Save rjmcdonald83/a4440ea6cc7a884bdf1f to your computer and use it in GitHub Desktop.
Save rjmcdonald83/a4440ea6cc7a884bdf1f to your computer and use it in GitHub Desktop.
var arr = [1,2,3,4];
for (var i = 0, len = arr.length; i < len; i++) {
var el = arr[i];
setTimeout(function () {
console.log(el);
}, 10);
}
// output:
// 4
// 4
// 4
// 4
var arr = [1,2,3,4];
arr.map(function (el) {
setTimeout(function () {
console.log(el);
}, 10)
})
// output:
// 1
// 2
// 3
// 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment