Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Comma Operator

The comma operator evaluates several expressions from left to right and returns only the last expression's value. Parentheses are often needed because comma has very low precedence.

How it works

The comma operator evaluates expressions from left to right and produces the value of the final expression. Earlier results are discarded, so those expressions are useful mainly when they cause side effects; parentheses are often required because the operator has very low precedence.

Syntax

expression1, expression2, ...

Parameters

NameTypeDescription
expressionsexpression listExpressions evaluated from left to right; the final expression determines the compound expression's value.

Returns

The value of the last expression.

Key points

  • It combines multiple expressions into one expression.
  • All operands run in left-to-right order, but only the final operand supplies the expression's value.
  • Earlier operand results do not survive; use them for effects rather than their values.
  • The operator binds very loosely, making grouping parentheses important in larger expressions.
  • Its unusual, limited role makes explicit statements preferable when they express the same logic more clearly.

Common mistakes

  • Because comma binds more weakly than assignment, parenthesize a comma expression when assigning its final result.
  • It is uncommon outside compact control-flow code, so separate statements may be clearer when no single expression is required.

Example: Evaluation and multiple loop updates

let value = (console.log("first"), 2 + 3);
console.log(value); // 5

for (let i = 0, j = 3; i < j; i++, j--) {
  console.log(i, j);
}

The first expression runs for its side effect, while the parenthesized expression evaluates to the final value. The loop uses the operator to update two variables.

When to use it

Multiple for-loop updates

A loop must update more than one variable after each iteration.

Compact side-effect expression

Earlier expressions need to run while one final value is required.

Interview questions

QWhat does the comma operator return?

It evaluates each operand from left to right, then returns the value of the last operand. Values produced by earlier operands are discarded.

Follow-up: What happens in (x++, x)? x++ runs first, then the updated value of x is returned by the second operand.

QWhy can parentheses be necessary with the comma operator?

Its precedence is lower than assignment, so a = 1, 2 is grouped as (a = 1), 2. To assign the final result of the comma expression, write a = (1, 2).

Follow-up: Why does a = (1, 2) assign 2? Parentheses force the comma expression to be evaluated before assignment.

QWhere is the comma operator commonly useful?

A common use is putting multiple update expressions in a for loop, such as i++, j--. The updates run left to right after each iteration.

Follow-up: What must earlier operands generally provide to be useful? They should normally have side effects, such as assignments, calls, or increment operations.

  • Basic operators, maths
  • Operator precedence
  • for statement
  • Parentheses

Source: Basic operators, maths. Checked 2026-10-15.