Skip to content

Latest commit

 

History

History
27 lines (21 loc) · 575 Bytes

prefer-includes.md

File metadata and controls

27 lines (21 loc) · 575 Bytes

Prefer .includes() over .indexOf() when checking for existence or non-existence

All built-ins have .includes() in addition to .indexOf(). Prefer .includes() over comparing the value of .indexOf()

This rule is fixable.

Fail

[].indexOf('foo') !== -1;
x.indexOf('foo') != -1;
str.indexOf('foo') > -1;
'foobar'.indexOf('foo') >= 0;
x.indexOf('foo') === -1

Pass

const str = 'foobar';
str.indexOf('foo') !== -n;
str.indexOf('foo') !== 1;
!str.indexOf('foo') === 1;
!str.indexOf('foo') === -n;
str.includes('foo');
[1,2,3].includes(4);