Skip to content

useIndexOf

Prefer Array#{indexOf,lastIndexOf}() over Array#{findIndex,findLastIndex}() when looking for the index of an item.

Array#findIndex() and Array#findLastIndex() are intended for more complex needs. If you are just looking for the index where the given item is present, then the code can be simplified to use Array#indexOf() or Array#lastIndexOf(). This applies to any search with a literal, a variable, or any expression that doesn’t have any explicit side effects. However, if the expression you are looking for relies on an item related to the function (its arguments, the function self, etc.), the case is still valid.

const index = foo.findIndex(x => x === 'foo');
code-block.js:1:15 lint/nursery/useIndexOf  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`

> 1 │ const index = foo.findIndex(x => x === ‘foo’);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

This callback only tests for equality against a single value. This value can be passed directly to indexOf() instead.

Unsafe fix: Replace Array#findIndex() with Array#indexOf()

1 - const·index·=·foo.findIndex(x·=>·x·===·foo);
1+ const·index·=·foo.indexOf(foo);
2 2

const index = foo.findIndex(x => 'foo' === x);
code-block.js:1:15 lint/nursery/useIndexOf  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`

> 1 │ const index = foo.findIndex(x => ‘foo’ === x);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

This callback only tests for equality against a single value. This value can be passed directly to indexOf() instead.

Unsafe fix: Replace Array#findIndex() with Array#indexOf()

1 - const·index·=·foo.findIndex(x·=>·foo·===·x);
1+ const·index·=·foo.indexOf(foo·);
2 2

const index = foo.findIndex(x => {
return x === 'foo';
});
code-block.js:1:15 lint/nursery/useIndexOf  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`

> 1 │ const index = foo.findIndex(x => {
^^^^^^^^^^^^^^^^^^^^
> 2 │ return x === ‘foo’;
> 3 │ });
^^
4 │

This callback only tests for equality against a single value. This value can be passed directly to indexOf() instead.

Unsafe fix: Replace Array#findIndex() with Array#indexOf()

1 - const·index·=·foo.findIndex(x·=>·{
2 - ·····return·x·===·foo;
3 - });
1+ const·index·=·foo.indexOf(foo);
4 2

const index = foo.findLastIndex(x => 'foo' === x);
code-block.js:1:15 lint/nursery/useIndexOf  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`

> 1 │ const index = foo.findLastIndex(x => ‘foo’ === x);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

This callback only tests for equality against a single value. This value can be passed directly to lastIndexOf() instead.

Unsafe fix: Replace Array#findLastIndex() with Array#lastIndexOf()

1 - const·index·=·foo.findLastIndex(x·=>·foo·===·x);
1+ const·index·=·foo.lastIndexOf(foo·);
2 2

const index = foo.findLastIndex(x => {
return x === 'bar';
});
code-block.js:1:15 lint/nursery/useIndexOf  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`

> 1 │ const index = foo.findLastIndex(x => {
^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ return x === ‘bar’;
> 3 │ });
^^
4 │

This callback only tests for equality against a single value. This value can be passed directly to lastIndexOf() instead.

Unsafe fix: Replace Array#findLastIndex() with Array#lastIndexOf()

1 - const·index·=·foo.findLastIndex(x·=>·{
2 - ·····return·x·===·bar;
3 - });
1+ const·index·=·foo.lastIndexOf(bar);
4 2

const index = foo.findLastIndex(function(x) {
return x === 'foo';
});
code-block.js:1:15 lint/nursery/useIndexOf  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`

> 1 │ const index = foo.findLastIndex(function(x) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ return x === ‘foo’;
> 3 │ });
^^
4 │

This callback only tests for equality against a single value. This value can be passed directly to lastIndexOf() instead.

Unsafe fix: Replace Array#findLastIndex() with Array#lastIndexOf()

1 - const·index·=·foo.findLastIndex(function(x)·{
2 - ·····return·x·===·foo;
3 - });
1+ const·index·=·foo.lastIndexOf(foo);
4 2

const index = foo.indexOf('foo');
const index = foo.findIndex(x => x !== 'foo');
const index = foo.findIndex((x, index) => x === index);
const index = foo.findIndex(x => (x === 'foo') && isValid());
const index = foo.findIndex(x => y === 'foo');
const index = foo.findIndex(x => y.x === 'foo');
const index = foo.findIndex(x => {
const bar = getBar();
return x === bar;
});
const index = foo.findIndex(function(x) {
const bar = getBar();
return x === bar;
});
const index = foo.lastIndexOf('foo');
const index = foo.findLastIndex(x => x !== 'foo');
const index = foo.findLastIndex((x, index) => x === index);
const index = foo.findLastIndex(x => (x === 'foo') && isValid());
const index = foo.findLastIndex(x => y === 'foo');
const index = foo.findLastIndex(x => y.x === 'foo');
biome.json
{
"linter": {
"rules": {
"nursery": {
"useIndexOf": "error"
}
}
}
}