This rule raises an issue when checking if any array element matches a condition using .filter().length, .find(), .findLast(), .findIndex(), or .findLastIndex() instead of the more appropriate .some() method.

Why is this an issue?

JavaScript’s Array.prototype.some() method is specifically designed to test whether at least one element in an array passes a provided test function. Using other array methods for this purpose creates several problems:

Semantic clarity: Methods like .filter().length > 0 or .find() !== undefined obscure the intent. When you read .some(condition), it’s immediately clear that you’re checking for existence. Other patterns require mental parsing to understand the purpose.

Performance implications: .filter() processes the entire array and creates a new array, even when you only need to know if one element matches. .some() short-circuits and stops as soon as it finds the first matching element, making it more efficient for large arrays.

Code maintainability: Using the wrong method for the job makes code harder to understand and maintain. Future developers (including yourself) need to spend extra time understanding why a more complex pattern was chosen over the straightforward .some() method.

Consistency: Using the semantically correct method makes your codebase more consistent and follows JavaScript best practices, improving overall code quality.

What is the potential impact?

Using inappropriate array methods for existence checking reduces code readability and can impact performance, especially with large arrays. While not a security issue, it makes code harder to maintain and understand, potentially leading to bugs during refactoring or when other developers work with the code.

How to fix?

Replace .filter().length checks with .some() to test for element existence. This is more readable and efficient since .some() stops at the first match.

Non-compliant code example

const hasUnicorn = array.filter(element => isUnicorn(element)).length > 0; // Noncompliant
const hasMatch = items.filter(item => item.active).length !== 0; // Noncompliant

Compliant code example

const hasUnicorn = array.some(element => isUnicorn(element));
const hasMatch = items.some(item => item.active);

Documentation