noCommaOperator
Diagnostic Category: lint/style/noCommaOperator
Since: v1.0.0
Sources:
- Same as:
no-sequences
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.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:1:27 lint/style/noCommaOperator ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The comma operator is disallowed.
> 1 │ const foo = (doSomething(), 0);
│ ^
2 │
ℹ Its use is often confusing and obscures side effects.
code-block.js:1:21 lint/style/noCommaOperator ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The comma operator is disallowed.
> 1 │ for (; doSomething(), !!test; ) {}
│ ^
2 │
ℹ Its use is often confusing and obscures side effects.
code-block.js:3:6 lint/style/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.