Behavior of `let` inside a loop

30 views
Skip to first unread message

Dickson Wong

unread,
Jan 11, 2015, 9:31:20 PM1/11/15
to traceur-comp...@googlegroups.com
I'm running into some strange behavior where a variable declared with `let` inside a loop retains the value from a previous iteration if it hasn't been initialized in the current iteration.

For instance, the following code:

for (var i of [0, 1]) {
  let x
;
 
if (i % 2 === 0) {
    x
= i;
 
}

  console
.log(`x: ${x}`);
}

Outputs:
x: 0
x
: 0


And more strangely, the following code:

for (var i of [0, 1]) {
  let x
;
 
if (i % 2 === 0) {
    x
= i;
 
}

  console
.log(`x: ${x}`);

 
var f = function() {
   
var y = x;
 
};
}

Outputs:
x: 0
x
: undefined


According to let declaration section of the ES6 draft, I was expecting a value of `undefined` in the second iteration in both cases.

I'm wondering if the ES6 spec specifies whether a variable declared using `let` inside a loop should be scoped to the current iteration of the loop or if it should be scoped to the loop block with the variable shared between all iterations.


Erik Arvidsson

unread,
Jan 12, 2015, 9:19:30 AM1/12/15
to traceur-comp...@googlegroups.com
This has been fixed:

https://github.com/google/traceur-compiler/issues/1567
> --
> You received this message because you are subscribed to the Google Groups
> "traceur-compiler-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to traceur-compiler-d...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
erik
Reply all
Reply to author
Forward
0 new messages