Skip to content

Latest commit

Β 

History

History
40 lines (27 loc) Β· 1.29 KB

File metadata and controls

40 lines (27 loc) Β· 1.29 KB

no-chained-comparison

πŸ“ 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).

Examples

// ❌
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)) {}