prefer-includes
Prefer .includes() over index-based existence checks.
Targeted pattern scopeโ
This rule targets array and string existence checks based on index comparison.
What this rule reportsโ
This rule reports patterns such as indexOf(...) !== -1 and equivalent
index-based presence checks.
Why this rule existsโ
.includes(...) is clearer and less error-prone than manual index
comparisons.
โ Incorrectโ
const hasValue =
[
1,
2,
3,
].indexOf(2) !== -1;
โ Correctโ
const hasValue = [
1,
2,
3,
].includes(2);
Behavior and migration notesโ
This rule forwards options and behavior to unicorn/prefer-includes.
- Lifecycle: Deprecated and frozen.
- Deprecated since:
v1.0.0 - Available until:
v2.0.0 - Use instead:
unicorn/prefer-includes
Additional examplesโ
const hasName = names.indexOf("alice") > -1;
// โ reported
const hasName2 = names.includes("alice");
// โ
valid
ESLint flat config exampleโ
import etcMisc from "eslint-plugin-etc-misc";
export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/prefer-includes": "error",
},
},
];
When not to use itโ
Disable this rule if you target runtimes where .includes() is unavailable and
cannot be polyfilled safely.
Package documentationโ
Rule catalog ID: R057
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.