consistent-existence-index-check

March 27, 2026 ยท View on GitHub

๐Ÿ“ Enforce consistent style for element existence checks with indexOf(), lastIndexOf(), findIndex(), and findLastIndex().

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

Enforce 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.

Similar to the explicit-length-check rule.

Examples

const index = foo.indexOf('bar');

// โŒ
if (index < 0) {}

// โœ…
if (index === -1) {}
const index = foo.indexOf('bar');

// โŒ
if (index >= 0) {}

// โœ…
if (index !== -1) {}
const index = foo.indexOf('bar');

// โŒ
if (index > -1) {}

// โœ…
if (index !== -1) {}
const index = foo.lastIndexOf('bar');

// โŒ
if (index >= 0) {}

// โœ…
if (index !== -1) {}
const index = array.findIndex(element => element > 10);

// โŒ
if (index < 0) {}

// โœ…
if (index === -1) {}
const index = array.findLastIndex(element => element > 10);

// โŒ
if (index < 0) {}

// โœ…
if (index === -1) {}