Redefining a let variable inside a for loop scope doesn't work?
Blake Regalia
blake.regalia at gmail.com
Thu Jul 14 20:36:54 UTC 2016
`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 <joe at trusktr.io> 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
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160714/12cfc5cc/attachment-0001.html>
More information about the es-discuss
mailing list