What does the comma operator do in JavaScript?

The comma operator evaluates both of
its operands (from left to right) and
returns the value of the second
operand.

Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator

For example, the expression 1,2,3,4,5 evaluates to 5. Obviously the comma operator is useful only for operations with side-effects.

console.log(1,2,3,4,5);
console.log((1,2,3,4,5));

Leave a Comment