ES6 grammar for for-loops

# Cyrus Najmabadi (10 years ago)

For the following code:

for (var i = 0 in {}) { }

We are trying to figure out what grammar productions allow the left hand side of a for-in statement to have an initializer.

I notice that in the ES5 spec, the following productions can get you there:

IterationStatement :
for ( var VariableDeclarationNoIn in Expression ) Statement
VariableDeclarationNoIn :

Identifier InitialiserNoInopt

But in ES6, all I see is

IterationStatement[Yield, Return] :
            for ( var ForBinding[?Yield] in Expression[In, ?Yield] ) Statement[?Yield, ?Return]
ForBinding[Yield] :
BindingIdentifier[?Yield]
BindingPattern[?Yield]

And neither BindingIdentifier or BindingPattern allow an initializer.

Is there a grammar path that I am missing?

# Leon Arnott (10 years ago)

That line of code looks like it came directly from a test I wrote for kangax's ES6 compatibility table. Let's look at the test in its entirety:

try { eval('for (var i = 0 in {}) {}'); } catch(e) { return true; }

The grammer you describe is correct: ES6 no longer supports this useless form. So, a conforming implementation must throw an error when the eval() string is evaluated. If it does so, the test returns true, signifiying conforming support.

I hope that answers your question - and pardon my presumptions if this test code wasn't what you were thinking of at all.

# Cyrus Najmabadi (10 years ago)

Ok. Thanks for confirming our read of the grammar. Is this shown on any breaking changes list anywhere?

# Brendan Eich (10 years ago)

It should be -- who is maintaining such a list?

Cc'ing Oliver, I believe he found a for (var x = i in o) use in the wild but it was evangelized successfully. Mike Taylor may have helped, cc'ing him too.

# Allen Wirfs-Brock (10 years ago)

people.mozilla.org/~jorendorff/es6-draft.html#sec-in-edition-6 and people.mozilla.org/~jorendorff/es6-draft.html#sec-in-the-6th-edition

the above is the 7th item on the second list.

But I know, there are some things still missing from these lists so please file a bug if you know of a breaking change that is missing