Skip to content

Instantly share code, notes, and snippets.

@ludekstepan
Last active August 29, 2015 14:17
Show Gist options
  • Save ludekstepan/8f7799cd8be8600266ee to your computer and use it in GitHub Desktop.
Save ludekstepan/8f7799cd8be8600266ee to your computer and use it in GitHub Desktop.
Block scoping of 'let' in for loop
"use strict";
var stack = [];
for (let i = 0; i < 10; i++) {
console.log("inner: " + i);
i += 4;
stack.push(() => console.log("outer: " + i));
}
stack.forEach(f => f());
inner: 0
inner: 1
inner: 2
inner: 3
inner: 4
inner: 5
inner: 6
inner: 7
inner: 8
inner: 9
outer: 4
outer: 5
outer: 6
outer: 7
outer: 8
outer: 9
outer: 10
outer: 11
outer: 12
outer: 13
"use strict";
var stack = [];
for (let i = 0; i < 10; i++) {
console.log("inner: " + i);
i += 4;
// stack.push(() => console.log("outer: " + i));
}
stack.forEach(f => f());
// Output:
// inner: 0
// inner: 5
"inner: 0"
"inner: 5"
"outer: 10"
"outer: 10"
node --version
v0.12.0
node --harmony for-let.js
inner: 0
inner: 5
outer: 4
outer: 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment