Psdd Psdd - 1 year ago 64
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download