Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 509 Bytes

valid-indexof-return.md

File metadata and controls

31 lines (22 loc) · 509 Bytes

Disallow boolean cast of returning value of .indexOf() (valid-indexof-return)

The .indexOf() methods for both String and Array return numeric types. In some cases, it can be confused with the .includes() method and used as a boolean type.

Fail

if (arr.indexOf(x)) {}
arr.indexOf(x) ? foo : bar
arr.indexOf(x) && arr.map(item => item.id)

Pass

if (arr.indexOf(x) !== -1) {}
arr.includes(x) ? foo : bar
arr.indexOf(x) + offset