π Prefer .includes() over repeated equality comparisons.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
Comparing the same expression against multiple values is easier to scan as a membership check.
This rule only reports strict equality comparisons joined by ||. It ignores optional chains, side-effectful compared values, and NaN values because an Array#includes() rewrite would not have the same behavior.
This rule does not autofix because the best rewrite depends on context. Plain member expressions are still reported, so consider accessors and proxies before rewriting.
// β
value === 'a' || value === 'b' || value === 'c';
// β
['a', 'b', 'c'].includes(value);// β
args[0] === '-h' || args[0] === '--help' || args[0] === '--version';
// β
['-h', '--help', '--version'].includes(args[0]);// β
value === 'a' || value === 'b';// β
value !== 'a' && value !== 'b';Comparing several distinct expressions against the same value is not reported, since there is no single subject to check for membership.
// β
state.a === undefined || state.b === undefined || state.c === undefined;Type: object
Type: integer
Minimum: 2
Default: 3
The minimum number of equality comparisons before reporting.
/* eslint unicorn/prefer-includes-over-repeated-comparisons: ["error", {"minimumComparisons": 4}] */
// β
value === 'a' || value === 'b' || value === 'c';
// β
value === 'a' || value === 'b' || value === 'c' || value === 'd';