This rule flags when Array.findIndex() or Array.findLastIndex() is used with a simple equality comparison that could be replaced with the more direct indexOf() or lastIndexOf() methods.

Why is this an issue?

The findIndex() and findLastIndex() methods are designed for complex search operations that require custom logic. When you only need to find the index of a specific value using strict equality comparison, these methods are unnecessarily complex.

Using indexOf() and lastIndexOf() for simple equality searches offers several advantages:

The findIndex() and findLastIndex() methods should be reserved for cases where you need custom comparison logic, use additional callback parameters (like index or array), or perform complex operations during the search.

What is the potential impact?

Using findIndex() for simple equality searches creates unnecessary complexity and may impact performance. While the functional difference is minimal, the code becomes harder to read and maintain. In performance-critical applications, the overhead of callback function execution can accumulate over many operations.

How to fix?

Replace findIndex() with indexOf() when searching for a specific value using strict equality comparison.

Non-compliant code example

const index = items.findIndex(x => x === 'target'); // Noncompliant

Compliant code example

const index = items.indexOf('target');

Documentation