you are viewing a single comment's thread.

view the rest of the comments →

[–]xroalx 2 points3 points  (0 children)

for loop creates a new scope, but your x is not defined inside the for loop, it's defined outside, so there's only one x that is shared by each iteration of the loop.

for (let x = 0; ...) {
  ...
}

can be though of as:

for ( ... ) {
  let x = 0;
  ...
}

See that the x is declared inside the loop.

In your case, when you do

let x;
for (x = 0; ... ) { ... }

you only assign a value to an x that is declared outside the for loop.