Redefining a let variable inside a for loop scope doesn't work?

/#!/JoePea joe at trusktr.io
Thu Jul 14 20:26:18 UTC 2016


The following examples are very confusing, and throw an error that `n` is
undefined:

```js
function go(n){
  for (let n of n.a) {
    console.log(n);
  }
}

go({a:[1,2,3]});
```

```js
let n = {a:[1,2,3]}
for (let n of n.a) {
  console.log(n);
}
```

Why does the `let n` in the for loop not create a new variable inside the
for loop scope? This seems to go against the intuitive expectation of using
`let`, which is to make variable scoping more clear and less error-prone,
so it seems that one would expect the above examples to create a new
variable `n` inside the scope.

Why is this not the case? Is it by design, and if so why?


*/#!/*JoePea
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160714/1a956535/attachment.html>


More information about the es-discuss mailing list