Skip to content

Latest commit

 

History

History
19 lines (13 loc) · 750 Bytes

prefer-starts-ends-with.md

File metadata and controls

19 lines (13 loc) · 750 Bytes

Prefer String#startsWith() & String#endsWith() over more complex alternatives

There are several ways of checking whether a string starts or ends with a certain string, such as string.indexOf('foo') === 0 or using a regex with /^foo/ or /foo$/. ES2015 introduced simpler alternatives named String#startsWith() and String#endsWith(). This rule enforces the use of those whenever possible.

This rule is partly fixable.

Fail

/^bar/.test(foo);
/bar$/.test(foo);

Pass

foo.startsWith('bar');
foo.endsWith('bar');