let in loops
# Andrea Giammarchi (11 years ago)
That is an intended behavior, you can see that let
loop in this way,
metaphorically speaking:
function test() {
for (var i=0; i<10; i++) {
(function(i){
setTimeout(() => console.log(i));
}.call(this, i));
}
}
except after the loop no variable i
with last loop value will be
accessible in that scope.
That is an intended behavior, you can see that `let` loop in this way, metaphorically speaking: ```js function test() { for (var i=0; i<10; i++) { (function(i){ setTimeout(() => console.log(i)); }.call(this, i)); } } ``` except after the loop no variable `i` with last loop value will be accessible in that scope. Regards On Mon, Aug 25, 2014 at 11:32 AM, Salvador de la Puente González < salva at unoyunodiez.com> wrote: > 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. > > _______________________________________________ > 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/20140825/9a8e91af/attachment.html>
# Salvador de la Puente González (11 years ago)
Does it only work for for
loops, or it is the same for any block?
Does it only work for `for` loops, or it is the same for any block? On Mon, Aug 25, 2014 at 11:46 AM, Andrea Giammarchi < andrea.giammarchi at gmail.com> wrote: > That is an intended behavior, you can see that `let` loop in this way, > metaphorically speaking: > > ```js > function test() { > for (var i=0; i<10; i++) { > (function(i){ > setTimeout(() => console.log(i)); > }.call(this, i)); > } > } > ``` > > except after the loop no variable `i` with last loop value will be > accessible in that scope. > > Regards > > > > On Mon, Aug 25, 2014 at 11:32 AM, Salvador de la Puente González < > salva at unoyunodiez.com> wrote: > >> 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. >> >> _______________________________________________ >> 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/20140825/a9ed2a0e/attachment.html>
# Rick Waldron (11 years ago)
On Mon, Aug 25, 2014 at 6:16 AM, Salvador de la Puente González < salva at unoyunodiez.com> wrote:
Does it only work for
for
loops,
No
or it is the same for any block?
Yes, let and const are block-scoped.
On Mon, Aug 25, 2014 at 6:16 AM, Salvador de la Puente González < salva at unoyunodiez.com> wrote: > Does it only work for `for` loops, > No or it is the same for any block? > Yes, let and const are block-scoped. Rick > > On Mon, Aug 25, 2014 at 11:46 AM, Andrea Giammarchi < > andrea.giammarchi at gmail.com> wrote: > >> That is an intended behavior, you can see that `let` loop in this way, >> metaphorically speaking: >> >> ```js >> function test() { >> for (var i=0; i<10; i++) { >> (function(i){ >> setTimeout(() => console.log(i)); >> }.call(this, i)); >> } >> } >> ``` >> >> except after the loop no variable `i` with last loop value will be >> accessible in that scope. >> >> Regards >> >> >> >> On Mon, Aug 25, 2014 at 11:32 AM, Salvador de la Puente González < >> salva at unoyunodiez.com> wrote: >> >>> 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. >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >> > > _______________________________________________ > 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/20140825/d1ea1073/attachment-0001.html>
Hello, recently I read about
let
inside for loops. According to leanpub.com/understandinges6/read#leanpub-auto-block-bindingslet
allows the developer to make: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 thelet
semantics?Thank you.