Skip to content

useConsistentExistenceIndexCheck (JavaScript)

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

Enforce comparing with -1 when checking whether indexOf(), lastIndexOf(), findIndex(), or findLastIndex() found an element.

These methods return -1 when they find nothing, so index === -1 and index !== -1 state the intent more clearly than index < 0, index >= 0, or index > -1.

The rule checks const variables that hold the result of one of these calls.

const index = list.indexOf(item);
if (index < 0) {}
code-block.js:2:11 lint/nursery/useConsistentExistenceIndexCheck  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This uses < 0 to check whether indexOf() found no element.

1 │ const index = list.indexOf(item);
> 2 │ if (index < 0) {}
│ ^^^
3 │

ℹ indexOf() returns -1 when it finds no element. Comparing the result with -1 makes it clear that the code checks for this value.

ℹ 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: Use === -1 instead.

1 1 │ const index = list.indexOf(item);
2 │ - if·(index·<·0)·{}
2 │ + if·(index·===·-1)·{}
3 3 │

const index = list.lastIndexOf(item);
if (index >= 0) {}
code-block.js:2:11 lint/nursery/useConsistentExistenceIndexCheck  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This uses >= 0 to check whether lastIndexOf() found an element.

1 │ const index = list.lastIndexOf(item);
> 2 │ if (index >= 0) {}
│ ^^^^
3 │

ℹ lastIndexOf() returns -1 when it finds no element. Comparing the result with -1 makes it clear that the code checks for this value.

ℹ 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: Use !== -1 instead.

1 1 │ const index = list.lastIndexOf(item);
2 │ - if·(index·>=·0)·{}
2 │ + if·(index·!==·-1)·{}
3 3 │

const index = list.findIndex((element) => element > 10);
if (index > -1) {}
code-block.js:2:11 lint/nursery/useConsistentExistenceIndexCheck  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This uses > -1 to check whether findIndex() found an element.

1 │ const index = list.findIndex((element) => element > 10);
> 2 │ if (index > -1) {}
│ ^^^^
3 │

ℹ findIndex() returns -1 when it finds no element. Comparing the result with -1 makes it clear that the code checks for this value.

ℹ 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: Use !== -1 instead.

1 1 │ const index = list.findIndex((element) => element > 10);
2 │ - if·(index·>·-1)·{}
2 │ + if·(index·!==·-1)·{}
3 3 │

const index = list.indexOf(item);
if (index === -1) {}
if (index !== -1) {}
// `let` and `var` variables can be reassigned, so they aren't checked.
let position = list.findLastIndex((element) => element > 10);
if (position < 0) {}