last day (14 days later) » 

14:33
1
A: ECMAScript 2015: const in for loops

Kit SundeYour second example should definitely not work because i is declared once and not on each iteration this is just a function of how that category of loops work. You can try this in a regular browser: for (var i = 0, otherVar = ""; i < [1,2,3,4].length; i += 1){ console.log(otherVar) otherVar...

How can you say that without citing the source? Why would that be the case for const but not for let?
@FelixKling Which statement do you think needs citation? const doesn't allow itself to be re-assigned (this isn't contested) and as my example clearly demonstrates how for works for the variable definition portion contrary to his implied expectations. The value of let can be changed, it's functionally equivalent to var in the example given, but let isn't part of the question.
So you explicitly mean that const i is only declared once, but let i wouldn't? Your example only demonstrates how var i works, not const i. Since there is a clear difference between var, const and let, I think citing he spec with respect to const would be very valuable.
@FelixKling You're misreading what I'm typing. I'm saying everything in that section of the for loop is declared once. Then orthogonally const value can only be assigned once, any attempt to redeclare const will not work. My example is to demonstrate the fact that the declaration only happens once, the person asking the questions understands the semantics of const. let isn't the issue, and no I didn't explicitly mean what you're suggesting, you misread.
But if "everything" is declared once, why do I get per iteration scoped variables if I use let? Or am I still misunderstanding you?
14:33
@FelixKling You can have let be iteration scoped and disallow const being redeclared. Run this in firefox: function test(){console.log("I'll be run once");} for(let i = 1, _ = test();i < 10;++i){console.log(i);}
Ah clicked that by mistake, but I might as well say here.
By iteration scoped I really mean across the execution of the for loop. Not that it's local to each iteration. These are functionally equivalent:

(function(){
for(var i = 0; i < 10; ++i){}
})();

for(let foo = 0; i < 10; ++i){}
15:07
That's where you are incorrect. If you use `let`, then each iteration gets its own copy of `i`.

for(let foo = 0; i < 10; ++i){}

is equivalent to

(funtion() {
for(var i = 0; i < 10; ++i){
(function(i) {

}(i))
}
}());

This is where I am coming from: let and const are both blocked scoped. `for/in` and `for/of` expose the same behavior for `const` and `let`, but the normal `for` loop does not.

You simply say that it is "declared once" but that simplifies it too much.
15:17
@FelixKling If each iteration had its own copy on let, then let wouldn't be mutable inside of the loop. It is.

for(let i = 0; i < 10; ++i){
console.log(i);
++i;
}

Will show:

0
2
4
6
8
My best reading of the ES2015 says that const should throw an exception, not a SyntaxError btw. I'm not sure why Firefox decides too. Because:

> Create a new but uninitialized immutable binding in an Environment Record. The String value N is the text of the bound name. If S is true then attempts to access the value of the binding before it is initialized or set it after it has been initialized will always throw an exception, regardless of the strict mode setting of operations that reference that binding. S is an optional parameter that defaults to false.
And it's of course not the case that `const` is always invalid. It's specifically about changing the value of a `const` variable.

These are valid:
for(const i = 0;;){ break }
for(const i = 0; i < 10;){ break; }

These are invalid:
for(const i = 0;;){ ++i; break; }
for(const i = 0;;++i){ break }
By which I mean I was reading all the cases for SyntaxError and as far as I can tell, none of them makes mention of a relevant case. Although it's something I'm overlooking.
15:49
Hmm, that is a tricky one. According to the spec, the value of the loop variable in the current iteration is initialized with the value of the loop variable in the previous environment. So the output you get seems to be expected, but you still get a copy per iteration. Proof:

var funcs = []
for(let i = 0; i < 3; ++i){
funcs.push(function() {
console.log(i);
++i;
})
}

funcs.forEach(function(f) {f();})

prints

0
1
2

whereas using `var` prints

3
4
5
It's not just initialized. I'm changing it from inside the loop.
Then ignore the ++i part.
Creating a closure shows that each iteration gets its own copy of i one way or the other.
var funcs = []
for(let i = 0; i < 3; ++i){
funcs.push(function() {
console.log(i);
})
}

funcs.forEach(function(f) {f();}) // 0,1,2

vs

var funcs = []
for(var i = 0; i < 3; ++i){
funcs.push(function() {
console.log(i);
})
}

funcs.forEach(function(f) {f();}) // 3,3,3
Hmm, I see what you mean.
And that's the same for for/in, but for both, const and let. However you can't simply replace let with const in this example, for the reasons you mentioned (you can't reassign to const).
Again, I'm not saying that what you wrote is wrong, it just feels a bit incomplete :)
Let me consider for a second
16:47
@FelixKling Okay I've created an example that makes a lot more sense to me:

`
(function() { // 1, 2, 3, 4
var foo = [];
for (let i = 0; i < 5; ++i) {
foo.push(function() {
// Outer i and this i is the same on the first iteration.
// On the next iteration they are different.
++i;
});
foo[0]();
console.log(i);
}
})();

`
(function() { // 1, 2, 3, 4, 5
for (let i = 0; i < 5;) {
// Modifies the reference.
(function() {
++i;
})();
console.log(i);
}
})();
`

My main point regarding const was that it's immutable, which is why what he was doing wouldn't work. Even if each iteration has a freshly s
Why can't I do code blocks in chat. `foo`

foo


```
foo
```
|-(

last day (14 days later) »