Psdd Psdd - 10 months ago 43
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) {


Answer Source

That's because

foo = true

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

    (Expression[In, ?Yield])


var foo = true

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