For example:
const aKeys = [];
for (let key of aKeys) {
...
}
Is transpiled to:
var aKeys = [];
for (var _i = 0, aKeys_1 = aKeys; _i < aKeys_1.length; _i++) {
var key = aKeys_1[_i];
}
What's the point of aKeys_1
here?
You can also view this live in Typescript playground here.
Because you could reassign aKeys
in the loop body, and it should not affect the iteration. Of course it's not necessary when you don't, but the transpiler doesn't know for sure.
And in general, as @Thomas observed in the comments, the expression must be evaluated only once - while it's trivial to see that it won't make a difference for a reference to a const
variable, in general it's not that easy.