Skip to content

useIncludes (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useIncludes": "error"
}
}
}
}

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.

invalid1.ts
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 │

invalid2.ts
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 │

invalid3.ts
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 │

invalid4.ts
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 │

invalid5.ts
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 fine
const pos = arr.indexOf(1);