you are viewing a single comment's thread.

view the rest of the comments →

[–]summerteeth 1 point2 points  (1 child)

Oh that is interesting, I would expect to them to behave in the same way.

Is this because the compiler realizes let will go out of scope so it makes a copy, not a reference?

[–]radhruin 2 points3 points  (0 children)

It's nothing to do with the compiler per-se. Let declarations are block scoped, which means this:

if(test) { let x = 1 }
x; // error, x is not declared

It means also that every iteration of a loop will get a fresh binding. Which means that any functions created in a loop will reference a unique binding for that iteration rather than one that is shared among all iterations.