Skip to content

Latest commit

 

History

History
19 lines (12 loc) · 716 Bytes

prefer-starts-ends-with.md

File metadata and controls

19 lines (12 loc) · 716 Bytes

Prefer String#startsWidth & String#endsWidth 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 regexes with /^foo/ or /foo$/. ES2015 introduced simpler alternatives named String#startsWith and String#endsWith. This rule enforces the use of those whenever possible.

Fail

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

Pass

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