Typically I'd use a let i
in the following first two cases:
> for (const i in ["red", "green", "blue"]) { console.log(i) }
0
1
2
> for (const i of ["red", "green", "blue"]) { console.log(i) }
red
green
blue
> for (const i = 0; i < 10; i++) { console.log(i) }
0
TypeError: Assignment to constant variable.
But the Mozilla documentation stated a const
can be used. So the i
ceased to exist after the end of the block { console.log(i) }
? Isn't that not true if it is the third case above?
Is it a subtle rule? The loop variable for for ( in )
and for ( of )
loop ceases to exist after the block, but for the for (;;)
loop, it stays? It is somewhat subtle -- is it in any of the ES6 specs?