Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 577 Bytes

prefer-includes.md

File metadata and controls

29 lines (21 loc) · 577 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);