`let n of n.a` is inside the `function` scope, not the `for` scope (look at the brackets). This is invalid/terrible-practice because `n` is already defined, and you are trying to use `let` to declare a new variable even though it already exists in the same scope.
- Blake On Thu, Jul 14, 2016 at 1:26 PM, /#!/JoePea <[email protected]> wrote: > 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 > > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss > >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

