Comma Operator Outside Conventional Contexts

ID

javascript.no_sequences

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

JavaScript

Tags

code-style, suspicious-comparison

Description

Reports the comma operator outside of the recognised contexts where it is conventional — a for loop’s init or update slot, an explicit parenthesised expression group, and a function call’s argument list. The comma operator evaluates each operand and discards all but the last; outside of those contexts, the shape almost always reads as a typo.

Rationale

  • if (a, b) { …​ } ignores a and tests only b — almost always a missing && or a stray comma.

  • return a, b; returns b and silently drops a — a missing semicolon or a copy-paste mistake.

  • Reviewers don’t expect comma-as-statement; the form is valid but rare and confusing.

// Bad - reads like a multi-test but only checks `b`
if (a, b) { ... }

// Bad - drops `a`
return a, b;

Remediation

Replace the comma with the intended operator (&&, ||, etc.), or break the expression into separate statements.

// Good
if (a && b) { ... }

doA();
return b;