Skip to content

arrayExistenceChecksConsistency

Reports inconsistent styles for checking element existence using index methods.

✅ This rule is included in the ts stylistic preset.

Enforces consistent style for element existence checks with indexOf(), lastIndexOf(), findIndex(), and findLastIndex().

Prefer using index === -1 to check if an element does not exist and index !== -1 to check if an element does exist. These methods return -1 when an element is not found, so checking against -1 is more explicit and precise than using broad comparisons like < 0 or >= 0.

const index = values.indexOf("test");
if (index < 0) {
// not found
}
const index = values.indexOf("test");
if (index >= 0) {
// found
}
const index = values.indexOf("test");
if (index > -1) {
// found
}

This rule is not configurable.

If your codebase has an established convention using < 0 or >= 0 for index checks, you may disable this rule. Some developers find the comparison style more readable in certain contexts.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.