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.
includes() was added in ES2015 and is the most readable way to test for a substring.
It takes an optional second argument, the index to start searching from:
A typical use is filtering a list:
indexOf() returns the zero-based index of the first match, or -1 if there is none.
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).
Neither method ignores case. Normalise both sides first.
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.
includes() rejects a regular expression and throws a TypeError. Use RegExp.prototype.test() or String.prototype.search() when you need a pattern.
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().
| Goal | Use |
|---|---|
| Only need yes or no | includes() |
| Need the index of the first match | indexOf() |
| Need the index of the last match | lastIndexOf() |
| Match a pattern | RegExp.test() |
| Check the start or end only | startsWith() or endsWith() |
| Ignore case | lowercase 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").
The methods exist only on strings, so calling them on null or undefined throws a TypeError. Guard the value or use optional chaining.
To count every occurrence, loop with the start index of indexOf():
Moving from forward by the length of the match avoids counting overlapping hits twice. If you want overlapping matches, advance by one instead.
Yes. "Hello".includes("hello") is false. Lowercase both strings if you want a case-insensitive check.
It returns -1. Always compare with !== -1 or >= 0, never use the result directly in an if.
No, it throws a TypeError. Use RegExp.test() or search() for patterns.
It is supported in all current browsers and Node.js. Only Internet Explorer lacks it, and that browser is no longer supported.
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.