Lint Rules

noCommaOperator (since v12.0.0)

Disallow comma operator.

The comma operator includes multiple expressions where only one is expected. It evaluates every operand from left to right and returns the value of the last operand. It frequently obscures side effects, and its use is often an accident.

The use of the comma operator in the initialization and update parts of a for is still allowed.

Source: https://eslint.org/docs/latest/rules/no-sequences

Examples

Invalid

const foo = doSomething(), 0;
nursery/noCommaOperator.js:1:28 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   expected an identifier, an array pattern, or an object pattern but instead found '0'
  
  > 1 │ const foo = doSomething(), 0;
                              ^
    2 │ 
  
   Expected an identifier, an array pattern, or an object pattern here
  
  > 1 │ const foo = doSomething(), 0;
                              ^
    2 │ 
  
for (; doSomething(), !!test; ) {}
nursery/noCommaOperator.js:1:21 lint/nursery/noCommaOperator ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The comma operator is disallowed.
  
  > 1 │ for (; doSomething(), !!test; ) {}
                       ^
    2 │ 
  
   Its use is often confusing and obscures side effects.
  
// Use a semicolon instead.
let a, b;
a = 1, b = 2;
nursery/noCommaOperator.js:3:6 lint/nursery/noCommaOperator ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The comma operator is disallowed.
  
    1 │ // Use a semicolon instead.
    2 │ let a, b;
  > 3 │ a = 1, b = 2;
        ^
    4 │ 
  
   Its use is often confusing and obscures side effects.
  

Valid

for(a = 0, b = 0; (a + b) < 10; a++, b += 2) {}