noCommaOperator
Summary
Section titled “Summary”- Rule available since:
v1.0.0
- Diagnostic Category:
lint/complexity/noCommaOperator
- This rule is recommended, which means is enabled by default.
- This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
no-sequences
- Same as
Description
Section titled “Description”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 “Examples”Invalid
Section titled “Invalid”const foo = (doSomething(), 0);
code-block.js:1:27 lint/complexity/noCommaOperator ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ The comma operator is disallowed.
> 1 │ const foo = (doSomething(), 0);
│ ^
2 │
ℹ Its use is often confusing and obscures side effects.
for (; doSomething(), !!test; ) {}
code-block.js:1:21 lint/complexity/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;
code-block.js:3:6 lint/complexity/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.
for(a = 0, b = 0; (a + b) < 10; a++, b += 2) {}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "complexity": { "noCommaOperator": "error" } } }}