Rule proposal: Prefer `String#includes` over `String#indexOf`
See original GitHub issueWith ES2015, it’s more succinct to use 'foobar'.includes('foo')
instead of 'foobar'.indexOf('foo') !== -1
or 'foobar'.indexOf('foo') >= 0
for checking whether a string contains a substring. I would like a rule that enforces this in those two scenarios and the negative cases of that: 'foobar'.indexOf('foo') === -1
and 'foobar'.indexOf('foo') < 0
.
Suggested rule name: prefer-string-includes
I think this would be a good rule as .includes
makes the code intent clearer.
There’s already other rules that enforces new ES2015 features, so it seems natural to have a rule for this too.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:19 (14 by maintainers)
Top Results From Across the Web
string.contains() doesn't exist while working in chrome
Browser compatibility of String.contains(). String.indexOf() is what I use and it will work fine. var strIndex = viewName.
Read more >Enforce includes method over indexOf method ( prefer- ...
If the receiver object of the indexOf method call has includes method and the two methods have the same parameters, this rule does...
Read more >JavaScript String Contains: Step-By-Step Guide
Let's look at the syntax for this method: string.indexOf(word);. Like in our includes() example, “string” refers to the value through which we ...
Read more >Guide on JavaScript Reduce: Different Use Cases Explained
The Basics of Applying JavaScript Reduce on Arrays ... Main syntax rules; 3. ... A callback function runs on every element in the...
Read more >Methods Reference
And so on up to c[5], which is set to the string "blast off". ... This string includes characters that the stringVar variable...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This can now be implemented as an option for
no-restricted-syntax
rule:So I don’t think this should be added to the core.
Okay, it’s been another three months without movement, so closing.