The ForInOfStatement production in the specification for ECMAScript is defined as follow for the cases I’m confused about:
for ( [lookahead ≠ let [] LeftHandSideExpression in Expression ) Statement
for ( [lookahead ∉ { let, async of }] LeftHandSideExpression of AssignmentExpression )
Disregard the “async of” difference, that is already well documented.
Why is there a difference in the let
vs let [
negative lookahead?
The for-in has let [
as negative lookahead, my assumption is to avoid this syntax to be covered by the line above:
for (let[a] in [1]) ;
If this was covered by the line above, it would mean “assign the key of [1]
to (let)[a]
“.
Instead, that syntax is covered by another row that makes it a let declaration with a array destruction pattern.
Which makes sense.
This is covered, and valid, by the line above in the for-in case:
for (let.a in [1]) ;
I.e. assign the key of [1]
to let.a
.
But this is not allowed for the for-of case:
for (let.b of [1]) ;
Firefox says:
Uncaught SyntaxError: an expression X in ‘for (X of Y)’ must not start with ‘let’
Node and chrome says:
SyntaxError: The left-hand side of a for-of loop may not start with ‘let’.
There must be some more unambiguity for the for-of loop that makes the restrictions wider for that one, what is that unambiguity?