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) {}