Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Comma operator (,)

The comma operator evaluates expressions from left to right and returns the last value. Side effects from earlier expressions are preserved. Do not confuse it with commas separating arguments, array elements, or declarations.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

(expression1, expression2)

The , operator evaluates its left operand, reads and discards its value, then evaluates its right operand and returns its value. A chain runs from left to right; side effects remain. If an earlier expression throws an exception, the next one is not evaluated.

Comma has the lowest precedence among JavaScript operators. Use parentheses to make the entire expression an initializer, for example: const result = (count += 2, count * 10). In the example, count becomes 3, and result receives 30.

Not every comma is an operator. In f(a, b), [a, b], and let a = 1, b = 2, commas are syntax separators. In contrast, f((a, b)) passes one argument: the value of b, after evaluating a.

The result is a value, not a property reference. Consequently, (0, obj.method)() does not preserve obj as the this receiver, unlike obj.method(). Use the operator sparingly; separate statements usually make sequential actions clearer.

Examples

let count = 1;
const result = (count += 2, count * 10);
console.log(count);
console.log(result);

Result

3
30

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources