In the MDN the comma operator is described:
The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.
But why does
var a, b = 3
return undefined
, while the expression
b = 3
will return 3, doesn't it?
This:
var a, b = 3;
is a VariableStatement. VariableStatement
evaluation in "normal completion" to empty
:
- Let next be the result of evaluating VariableDeclarationList.
- ReturnIfAbrupt(next).
- Return NormalCompletion(empty).
This:
b = 3;
is an ExpressionStatement. ExpressionStatement
evaluates to the result of the evaluating expression:
- Let exprRef be the result of evaluating Expression.
- Return ? GetValue(exprRef).