Skip to content

useIncludes

biome.json
{
"linter": {
"rules": {
"nursery": {
"useIncludes": "error"
}
}
}
}

Prefer Array#includes() over Array#indexOf() checks.

Array#indexOf() returns a numeric index and is commonly compared against -1 to check for the presence of an element. Array#includes() is more readable and expressive for this purpose, 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

const arr = [1, 2, 3];
arr.includes(1);
const arr = [1, 2, 3];
// Positional use of indexOf is fine
const pos = arr.indexOf(1);