Skip to content

Files

Latest commit

 

History

History
27 lines (19 loc) · 592 Bytes

consistent-existence-index-check.md

File metadata and controls

27 lines (19 loc) · 592 Bytes

Pattern: Inconsistent index existence check

Issue: -

Description

When checking for element existence using methods like indexOf(), lastIndexOf(), findIndex(), and findLastIndex(), use strict equality comparisons with -1 for consistency and clarity.

Examples

Example of incorrect code:

const index = foo.indexOf("bar");
if (index < 0) {}

const index = foo.indexOf("bar");
if (index >= 0) {}

Example of correct code:

const index = foo.indexOf("bar");
if (index === -1) {}

const index = foo.indexOf("bar");
if (index !== -1) {}