π Disallow two comparisons of the same operands that can be combined into one.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π‘ This rule is manually fixable by editor suggestions.
When a || or && joins two comparisons of the same pair of operands, the pair often says the same thing as a single comparison. x === y || x < y is exactly x <= y. Combining them removes noise and avoids evaluating the operands twice.
The operands may appear in either order. x === y || y < x is also x >= y; the suggestion keeps the operand order of the first comparison.
// β
x === y || x < y;
// β
x <= y;// β
x === y || x > y;
// β
x >= y;// β
x <= y && x >= y;
// β
x === y;// β
x <= y && x !== y;
// β
x < y;// β β operands in the other order
x === y || y < x;
// β
x >= y;| Code | Combined |
|---|---|
x === y || x < y |
x <= y |
x === y || x > y |
x >= y |
x < y || x > y |
x !== y |
x <= y && x >= y |
x === y |
x <= y && x !== y |
x < y |
x >= y && x !== y |
x > y |
The replacements are offered as suggestions, not autofixes, because some reductions can change runtime behavior for valid JavaScript values. For example, undefined === undefined || undefined < undefined is true, but undefined <= undefined is false.
The reductions assume the operands are the same primitive type, which is the common case. They are not exact for mixed types or objects with a custom valueOf, because the relational operators (<, <=, >, >=) coerce their operands while === and !== do not. For example, with x = '5', x === 5 || x < 5 is false but x <= 5 is true. Such comparisons are unusual and typically a mistake, so the rule still reports them, but only as suggestions. Only strict equality (===/!==) is recognized; loose equality (==/!=) is left alone.
The rule does not flag a comparison that is fully implied by the other (x <= y || x < y) or a flipped pair without an equality check (x < y || y > x), since these are easy to write intentionally. It also only combines two comparisons at a time, so a longer chain is simplified pairwise rather than collapsed (x < y || x === y || x > y becomes x <= y || x > y).