Comma operator
The comma operator (,
) evaluates both of its operands and returns the value of the last operand. This operator is primarily used inside a for
loop, to allow multiple variables to be updated each time through the loop. It is regarded bad style to use it elsewhere, when it is not necessary. Often two separate statements can and should be used instead.
For example, if a
is a 2-dimensional array with 10 elements on a side, the following code uses the comma operator to update two variables at once. The code prints the values of the diagonal elements in the array:
JSCopy to Clipboard
Last updated