Reputation: 18781
Maybe I'm just not understanding the es6 'let' keyword.
Wy would iterate(10)
only iterate 4 times? Why is the final output 15?
function iterate(count){
for(let i = 0; i < count; i++){
i += i
console.log('inside', i);
}
console.log('outside', i);
}
iterate(10);
//0
//inside 2
//inside 6
//inside 14
//outside 15
How should I go about using let
in a for
loop? When should I think to use let
.
Upvotes: 1
Views: 100
Reputation: 193261
why would iterate(10) only iterate 4 times?
Because you increment i
by itself, basically multiply by two in each iteration:
i += i
It has nothing to do with let
. The same result would be with var
.
why final output be 15?
That should actually throw a Reference error because i
is not available outside of the loop.
Upvotes: 3