When let
is used in the initialiser expression of a traditional for
loop, it is scoped to the block of the for loop.
The spec says:
let
andconst
declarations define variables that are scoped to the running execution context'sLexicalEnvironment
.
Does that mean that the point of declaration of i
is (and even in ES5 was(?)) lexically (semantically I know using var
it would be hoisted) inside the block comprising the body of the for
loop (because naively it looks like it is outside it).
Or does it mean that this is a new feature of let
and/or the for
loop to give it the semantics of being lexically inside the loop body?
To be clear: I know that the visibility semantics are new (i.e. block scoping for let
vs function scooping for var
). I am interested in whether the lexical position of definition has always been taken to be inside the body of the loop.
for(let i = 0; i < 10; i++) {
// the body of the loop....
}