question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: Prefer `String#includes` over `String#indexOf`

See original GitHub issue

With 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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:19 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
ilyavolodincommented, Mar 28, 2017

This can now be implemented as an option for no-restricted-syntax rule:

"no-restricted-syntax": ["error", "BinaryExpression > CallExpression > MemberExpression > Identifier[name = 'indexOf']"]

So I don’t think this should be added to the core.

2reactions
nzakascommented, Aug 3, 2016

Okay, it’s been another three months without movement, so closing.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found