useIncludes (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.5.0 - Diagnostic Category:
lint/nursery/useIncludes - This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
@typescript-eslint/prefer-includes - Inspired from
unicorn/prefer-includes - Inspired from
e18e/prefer-includes
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useIncludes": "error" } } }}Description
Section titled “Description”Prefer Array#includes() over Array#indexOf(), Array#lastIndexOf(), and Array#some() when checking for existence or non-existence.
Array#indexOf() and Array#lastIndexOf() return a numeric index and are commonly compared
against -1 to check for the presence of an element. Array#some() is sometimes used with a
strict-equality callback for the same purpose. Array#includes() is more readable and
expressive, and avoids off-by-one mistakes with the comparison operator.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const arr = [1, 2, 3];arr.indexOf(1) !== -1;/invalid1.ts:2:1 lint/nursery/useIncludes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Checking the result of indexOf() against -1 to test for presence.
1 │ const arr = [1, 2, 3];
> 2 │ arr.indexOf(1) !== -1;
│ ^^^^^^^^^^^^^^^^^^^^^
3 │
ℹ indexOf() returns a numeric index, not a boolean. Comparing it against -1 is error-prone and harder to read.
ℹ Use includes() instead, which directly expresses the intent and returns a boolean.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with .includes().
1 1 │ const arr = [1, 2, 3];
2 │ - arr.indexOf(1)·!==·-1;
2 │ + arr.includes(1);
3 3 │
const arr = [1, 2, 3];arr.indexOf(1) >= 0;/invalid2.ts:2:1 lint/nursery/useIncludes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Checking the result of indexOf() against -1 to test for presence.
1 │ const arr = [1, 2, 3];
> 2 │ arr.indexOf(1) >= 0;
│ ^^^^^^^^^^^^^^^^^^^
3 │
ℹ indexOf() returns a numeric index, not a boolean. Comparing it against -1 is error-prone and harder to read.
ℹ Use includes() instead, which directly expresses the intent and returns a boolean.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with .includes().
1 1 │ const arr = [1, 2, 3];
2 │ - arr.indexOf(1)·>=·0;
2 │ + arr.includes(1);
3 3 │
const arr = [1, 2, 3];arr.indexOf(1) === -1;/invalid3.ts:2:1 lint/nursery/useIncludes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Checking the result of indexOf() against -1 to test for presence.
1 │ const arr = [1, 2, 3];
> 2 │ arr.indexOf(1) === -1;
│ ^^^^^^^^^^^^^^^^^^^^^
3 │
ℹ indexOf() returns a numeric index, not a boolean. Comparing it against -1 is error-prone and harder to read.
ℹ Use !...includes() instead, which directly expresses the intent and returns a boolean.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with !...includes().
1 1 │ const arr = [1, 2, 3];
2 │ - arr.indexOf(1)·===·-1;
2 │ + !
3 │ + arr.includes(1);
3 4 │
const arr = [1, 2, 3];arr.lastIndexOf(1) !== -1;/invalid4.ts:2:1 lint/nursery/useIncludes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Checking the result of lastIndexOf() against -1 to test for presence.
1 │ const arr = [1, 2, 3];
> 2 │ arr.lastIndexOf(1) !== -1;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
3 │
ℹ lastIndexOf() returns a numeric index, not a boolean. Comparing it against -1 is error-prone and harder to read.
ℹ Use includes() instead, which directly expresses the intent and returns a boolean.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with .includes().
1 1 │ const arr = [1, 2, 3];
2 │ - arr.lastIndexOf(1)·!==·-1;
2 │ + arr.includes(1);
3 3 │
const arr = [1, 2, 3];arr.some((item) => item === 1);/invalid5.ts:2:1 lint/nursery/useIncludes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Using some() with a strict-equality callback to test for presence.
1 │ const arr = [1, 2, 3];
> 2 │ arr.some((item) => item === 1);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │
ℹ Use includes() instead, which directly expresses the intent and returns a boolean.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with .includes().
1 1 │ const arr = [1, 2, 3];
2 │ - arr.some((item)·=>·item·===·1);
2 │ + arr.includes(1);
3 3 │
const arr = [1, 2, 3];
arr.includes(1);
!arr.includes(1);
// Positional use of indexOf is fineconst pos = arr.indexOf(1);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.