Comma operator (,)
Evaluates each operand left to right and returns the value of the last.
Syntax
expr1, expr2, ..., exprN
Returns
any — The value of the last (rightmost) expression.
Examples
const x = (1, 2, 3);
console.log(x);
Output
3
let a = 0;
const b = (a++, a + 10);
console.log(a, b);
Output
1 11
// Multiple updates in a for loop
for (let i = 0, j = 3; i < j; i++, j--) {
console.log(i, j);
}
Output
0 3
1 2
Notes
- All operands are evaluated, but only the last value is produced.
- Commonly seen in `for` loop update clauses.
- Has the lowest precedence; wrap in parentheses when assigning.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |