let in loops

Salvador de la Puente González salva at unoyunodiez.com
Mon Aug 25 02:32:29 PDT 2014


Hello, recently I read about `let` inside for loops. According to
https://leanpub.com/understandinges6/read#leanpub-auto-block-bindings

`let` allows the developer to make:

```js
function test() {
  for (let i=0; i<10; i++) {
    setTimeout(() => console.log(i));
  }
}
```

And this will print 0, 1, 2... instead of 10, ten times. Why? Is `let`
involving more than a "avoid-hoisting" behavior? Is it an intended behavior
or is a side effect in the `let` semantics?

Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140825/85a09934/attachment.html>


More information about the es-discuss mailing list