π Prefer comparing values directly over subtracting and comparing to 0.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§π‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
Subtracting two values only to compare the difference with 0 obscures the intent. Comparing the values directly is shorter and clearer.
This pattern is often a leftover from code that previously used a comparator function (for example, (a, b) => a - b).
// β
if (a - b > 0) {}
// β
if (a > b) {}// β
if (a - b <= 0) {}
// β
if (a <= b) {}// β
if (0 < a - b) {}
// β
if (a > b) {}// β
if (a - b === 0) {}
// β
if (a === b) {}The rewrite is only behavior-preserving for numbers. For example, "10" - "5" > 0 is true, but "10" > "5" is false, since strings compare lexicographically. The rule auto-fixes strict ordering comparisons (> and <) when both operands are known to be numbers. For non-strict ordering and equality comparisons, it auto-fixes only when both operands are statically known finite numbers. Otherwise, the rule offers a suggestion instead of an auto-fix.
The extra restriction for non-strict ordering and equality comparisons avoids the infinity edge case: Infinity - Infinity is NaN, so Infinity - Infinity >= 0 is false while Infinity >= Infinity is true.