WTEDST
WTEDST

Reputation: 955

What expression statements in ECMAScript actually are

Let's say that at some point of a parsing process, I've already traversed the following grammar derivation:

Script -> ScriptBody -> StatementList -> ExpressionStatement -> Expression

Here comes the trouble. According to the spec, an Expression within an ExpressionStatement can only end up as an AssignmentExpression or a sequence of those. However, statements like

a;
12;
"some text";

definitely are expression statements (as far as I understand), at the same time not being any of the AssignmentExpression's possible subtypes. They do not resolve to a syntax error in my browsers.

Is it a particular implementation's or engine's feature that these statements return the value or am I missing something in the spec? Maybe, those are not AssignmentExpressions at all and I'm mistaking them for something else?

Upvotes: 1

Views: 92

Answers (1)

snak
snak

Reputation: 6703

When you take a look at the definition of AssignmentExpression, you'll see that it resolves to , for example, AssignmentExpression -> ConditionalExpression -> LogicalORExpression -> LogicalANDExpression -> BitwiseORExpression -> BitwiseXORExpression -> BitwiseANDExpression -> EqualityExpression -> RelationalExpression -> ShiftExpression -> AdditiveExpression -> MultiplicativeExpression -> ExponentiationExpression -> UnaryExpression -> UpdateExpression -> LeftHandSideExpression -> NewExpression -> MemberExpression -> PrimaryExpression -> Literal -> StringLiteral.

So "some text" is a valid AssignmentExpression. The same goes for other literals and variable references.

Upvotes: 3

Related Questions