π Disallow chained comparisons such as a < b < c.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π‘ This rule is manually fixable by editor suggestions.
Unlike in math (or Python), chained comparisons in JavaScript do not check a range. Comparison operators are binary and left-associative, so a < b < c parses as (a < b) < c. The first comparison evaluates to a boolean, which is then coerced to 0 or 1 and compared with c. This is almost always a bug.
Use && to compare each pair of operands separately.
This rule leaves intentional patterns alone: comparing a comparison result against another boolean value with an equality operator, such as (a > 0) === (b > 0), (a < b) === true, or (a > 0) !== Boolean(b).
// β
if (a < b < c) {}
// β
if (a < b && b < c) {}// β
if (a === b === c) {}
// β
if (a === b && b === c) {}// β
// Intentionally comparing two boolean results.
if ((a > 0) === (b > 0)) {}