Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you check if a string contains a substring?

JavaScript
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 30, 2026
5 min read

To check whether a JavaScript string contains a substring, call str.includes(search), which returns true or false. If you also need the position of the match, use indexOf(), which returns the index or -1 when nothing is found.

Both methods are case-sensitive and search for exact text, not patterns. This guide shows when to use each one and how to handle case and regular expressions.

Table of Contents

  • Using javascript string includes
  • Finding the position with indexOf
  • Case-insensitive search
  • Searching with regular expressions
  • Which method should you use
  • Handling null input and counting matches
  • Frequently Asked Questions
    • Is includes case-sensitive in JavaScript?
    • What does indexOf return when the substring is missing?
    • Can includes take a regular expression?
    • Does includes work on older browsers?
  • Summary
  • Related Articles

Using javascript string includes

includes() was added in ES2015 and is the most readable way to test for a substring.

javascript
javascript
Loading syntax highlighting...

It takes an optional second argument, the index to start searching from:

javascript
javascript
Loading syntax highlighting...

A typical use is filtering a list:

javascript
javascript
Loading syntax highlighting...

Finding the position with indexOf

indexOf() returns the zero-based index of the first match, or -1 if there is none.

javascript
javascript
Loading syntax highlighting...

Before includes() existed, the idiom was s.indexOf(x) !== -1. It still works, but includes() says what you mean. A classic bug is writing if (s.indexOf("a")), which is false when the match is at index 0 and true when it is not found (because -1 is truthy).

javascript
javascript
Loading syntax highlighting...

Case-insensitive search

Neither method ignores case. Normalise both sides first.

javascript
javascript
Loading syntax highlighting...

For user-facing search in many languages, localeCompare and Intl.Collator with sensitivity: "base" handle accents and locale rules better than toLowerCase(), but for simple filters lowercasing is enough.

Searching with regular expressions

includes() rejects a regular expression and throws a TypeError. Use RegExp.prototype.test() or String.prototype.search() when you need a pattern.

javascript
javascript
Loading syntax highlighting...

Do not pass user input straight into new RegExp(); special characters such as . or ( change the meaning. Escape the text first, or just use includes().

Which method should you use

GoalUse
Only need yes or noincludes()
Need the index of the first matchindexOf()
Need the index of the last matchlastIndexOf()
Match a patternRegExp.test()
Check the start or end onlystartsWith() or endsWith()
Ignore caselowercase both sides, then includes()

The same includes() name exists on arrays, where it checks for an element using strict equality (with NaN handled), so do not confuse "abc".includes("b") with ["a", "b"].includes("b").

javascript
javascript
Loading syntax highlighting...

Handling null input and counting matches

The methods exist only on strings, so calling them on null or undefined throws a TypeError. Guard the value or use optional chaining.

javascript
javascript
Loading syntax highlighting...

To count every occurrence, loop with the start index of indexOf():

javascript
javascript
Loading syntax highlighting...

Moving from forward by the length of the match avoids counting overlapping hits twice. If you want overlapping matches, advance by one instead.

Frequently Asked Questions

Is includes case-sensitive in JavaScript?

Yes. "Hello".includes("hello") is false. Lowercase both strings if you want a case-insensitive check.

What does indexOf return when the substring is missing?

It returns -1. Always compare with !== -1 or >= 0, never use the result directly in an if.

Can includes take a regular expression?

No, it throws a TypeError. Use RegExp.test() or search() for patterns.

Does includes work on older browsers?

It is supported in all current browsers and Node.js. Only Internet Explorer lacks it, and that browser is no longer supported.

Summary

Use includes() for a simple true or false answer and indexOf() when you need the position, remembering that it returns -1 for no match. Both are case-sensitive and take an optional start index. For patterns, switch to a regular expression with test(), and normalise case yourself when searching without regard to case.

Related Articles

  • What are template literals in JavaScript?
  • How do startsWith and endsWith work in JavaScript?
  • Learn JavaScript: Complete Study Roadmap, Beginner to Advanced
Share

Similar posts

  • How do startsWith and endsWith work in JavaScript?
  • How do you get string length and access characters?
  • How do you create strings and escape characters in JavaScript?
  • How do you solve FizzBuzz in JavaScript?
  • How do break and continue work in JavaScript loops?
  • How does the while loop work in JavaScript?
  • How does the for loop work in JavaScript?
  • How do you use a switch statement in JavaScript?