noCommaOperator (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled βSummaryβ- Rule available since:
v1.0.0 - Diagnostic Category:
lint/complexity/noCommaOperator - This rule is recommended, meaning it 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
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "complexity": { "noCommaOperator": "error" } } }}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) {}Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.