Psdd Psdd - 18 days ago 11
Javascript Question

Why the "var" keyword can't be used with the comma operator?

The following spinet is valid JS:

var foo;
if (foo = true, foo) {
}


But this one isn't:

if ((var foo = true), foo) {
}


Why?

Answer

That's because

foo = true

is an AssignmentExpression, which is an Expression, so you can wrap it inside a ParenthesizedExpression:

ParenthesizedExpression[Yield]:
    (Expression[In, ?Yield])

But

var foo = true

is a VariableStatement, which is a Statement, not an Expression.

Comments