JavaScript - Comma Operator
The JavaScript comma , operator evaluates each of its operands (from left to right) and returns the value of the last operand. This facilitates to create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.
The comma operator has left-to-right associativity. Two expressions separated by a comma are evaluated left to right.
Example:
In the example below, the comma , operator is used to evaluate multiple expressions and returns the value of the last operand.
var x = 10; var y = 10; var txt; //using comma operator var A = (x++, x); var B = (y+=5, y*=2, y); //displaying result txt = "(x++, x) : " + A + "<br>"; txt = txt + "(y+=5, y*=2, y) : " + B;
The output (value of txt) after running above script will be:
(x++, x) : 11 (y+=5, y*=2, y) : 30
❮ JavaScript - Operators