Skip to main content

prefer-less-than

Disallow greater-than comparisons in favor of less-than comparisons.

Targeted pattern scopeโ€‹

This rule targets binary comparisons that use > or >=.

What this rule reportsโ€‹

This rule reports > and >= expressions and provides fixes that swap operands and convert operators to < or <=.

Why this rule existsโ€‹

Some teams prefer comparison chains that read in ascending order (for example, min <= value && value <= max). Enforcing less-than style keeps range checks visually consistent across a codebase.

โŒ Incorrectโ€‹

const isValid = value > min;

if (value >= min && value <= max) {
run();
}

โœ… Correctโ€‹

const isValid = min < value;

if (min <= value && value <= max) {
run();
}

Behavior and migration notesโ€‹

This rule has no options.

The rule is auto-fixable. The fixer swaps both operands and rewrites the operator:

  • > โ†’ <
  • >= โ†’ <=

Additional examplesโ€‹

if (score >= threshold) {
pass();
}
// โŒ reported and auto-fixable to `threshold <= score`

if (threshold <= score) {
pass();
}
// โœ… valid

ESLint flat config exampleโ€‹

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/prefer-less-than": "error",
},
},
];

When not to use itโ€‹

Disable this rule if your team intentionally uses greater-than comparisons for readability and does not want operand-swapping autofixes.

Package documentationโ€‹

Rule catalog ID: R059

Further readingโ€‹

Adoption resourcesโ€‹

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.