JavaScript String includes Explained With Examples
String includes() answers whether one string appears inside another. See position, case sensitivity, the RegExp error and Unicode edge cases.

includes() looks like the safest string method there is: it returns a boolean, it never throws on a miss, and it reads like English. The trouble starts with what you did not ask it. "administrator".includes("admin") is true, "Admin".includes("admin") is false, and "café".includes("e") depends on how the é was typed. Each answer is correct by the rules, and each one has caused a permissions check or a search filter to behave strangely in production. This post covers what includes() takes, how the match is decided, and where substring checks are the wrong question. Everything runs on Node.js 24. If you need the position of the match and not only a yes or no, read [the post on String.prototype.indexOf()](/library/javascript-string-indexof) next.
What includes() takes and returns
The signature is string.includes(searchString, position). It checks whether searchString occurs anywhere in the string at or after position, and it returns true or false.
| Argument | Meaning | Default |
|---|---|---|
searchString | The text to look for. Anything that is not a string is converted to one | none |
position | The index to start looking from | 0 |
The method never changes the original string, because strings are immutable, and it never returns anything but a boolean. The comparison is made code unit by code unit, so it is case sensitive and it does not know about words, locales or accents.
const title = "Learning JavaScript";
console.log(title.includes("Java"));
console.log(title.includes("java"));
console.log(title.includes("Java", 9));
console.log(title.includes("Java", 10));true
false
true
falseThe word Java begins at index 9, so a search that starts at 9 still sees it and a search that starts at 10 does not. The lowercase form fails on the second line because j and J are different code units.
The position argument
position is converted to an integer. A negative value, NaN or anything below zero is treated as 0, and a value larger than the length is treated as the length. That clamping means the argument never throws, but it also means a typo such as passing a negative offset thinking it counts from the end does nothing useful.
const word = "banana";
console.log(word.includes("ban", -5));
console.log(word.includes("ban", NaN));
console.log(word.includes("a", 100));true
true
falseThe first two calls start at 0, so ban is found. The third starts at the length, where nothing remains to match. Counting matters when you pass a real offset. In "banana" the letters sit at b(0) a(1) n(2) a(3) n(4) a(5), so ana begins at both 1 and 3.
const word = "banana";
console.log(word.includes("ana", 1));
console.log(word.includes("ana", 3));
console.log(word.includes("ana", 4));true
true
falseFrom index 4 only na remains, which is too short for a three character search. When you pass position, count the indexes by hand, because the method will not tell you that you skipped past the match.
Empty strings and missing arguments
An empty search string is found in every string, at every position, including past the end.
console.log("abc".includes(""));
console.log("".includes(""));
console.log("abc".includes("", 99));true
true
trueThis surprises code that builds the search term from user input. An empty search box turns into includes(""), which matches everything, and that is often the behavior you want for a filter. It is a problem only when you use the boolean to mean "the user typed something that matched".
When the argument is not a string, it is converted with the usual string conversion. Omitting it passes undefined, which becomes the text "undefined".
console.log("abc".includes());
console.log("value: undefined".includes());
console.log("id 42".includes(42));
console.log("a,b".includes(["a", "b"]));false
true
true
trueThe second line is the one that bites: a string that merely mentions the word undefined passes a check that was meant to fail on a forgotten argument. The fourth line works because an array converts to "a,b". We would not rely on that conversion in real code. Convert deliberately with String(value) so the reader sees the intent.
Regular expressions are rejected
includes() refuses a regular expression. The specification checks for one and throws, so that a future version could give regexes a meaning without silently changing existing code.
try {
"abc".includes(/b/);
} catch (err) {
console.log(err.name);
console.log(err.message);
}TypeError
First argument to String.prototype.includes must not be a regular expressionIf you need a pattern, use RegExp.prototype.test() or [String.prototype.match()](/library/javascript-string-match). If you have a regex-looking object that you want treated as plain text, convert it first with String(pattern). Note that the check is made through the Symbol.match property, so an object that sets that property to a truthy value is also rejected.
Case and accents
Because the comparison is exact, case-insensitive matching is something you do before the call. Lowercasing both sides is the usual approach.
const haystack = "Release Notes";
const needle = "NOTES";
console.log(haystack.includes(needle));
console.log(haystack.toLowerCase().includes(needle.toLowerCase()));false
trueLowercasing is fine for English identifiers and labels. For user-facing text in several languages, toLowerCase() is not a complete answer, since some languages have case rules that depend on locale. In that situation, [localeCompare()](/library/javascript-string-localecompare) with a sensitivity option is the sturdier tool for comparing whole strings, though it cannot search inside one.
Accents bring a second problem. The character é can be written as one code point, é, or as e followed by a combining accent, ́. They look identical and they are different strings.
const composed = "café";
const decomposed = "café";
console.log(composed.includes("e"));
console.log(decomposed.includes("e"));
console.log(composed.length);
console.log(decomposed.length);
console.log(composed.normalize("NFD").includes("e"));false
true
4
5
trueThe composed form has four code units and no plain e. The decomposed form has five, and the fourth is a plain e, so a search for e succeeds. Calling [normalize()](/library/javascript-string-normalize) on both the text and the search term, with the same form, removes the difference. If you want accent-insensitive matching, normalize to NFD and strip the combining marks with a regex before searching.
Code units, not characters
Strings are sequences of UTF-16 code units. Characters outside the Basic Multilingual Plane, such as most emoji, occupy two units. includes() compares units, so it can find half of one.
const face = "\u{1F600}";
console.log(face.length);
console.log(face.includes("\ud83d"));
console.log([...face].length);2
true
1A search for the lone high surrogate succeeds, which is rarely what you meant. If the text may contain lone surrogates that break encoders, [isWellFormed()](/library/javascript-string-iswellformed) tells you before you search or send it over the network.
A mistake we hit: substring is not membership
The most common failure is using includes() where you want membership in a list of words. It checks for a substring, not a token.
const allowed = "admin,editor";
console.log(allowed.includes("admin"));
console.log(allowed.includes("edit"));
console.log(allowed.includes("min,ed"));true
true
trueThe last two lines are bugs if allowed was meant to be a role list. edit is not a role, and min,ed is not either, yet both pass. The fix is to keep the roles in an array and use Array.prototype.includes(), which compares whole elements, or to split first.
const roles = "admin,editor".split(",");
console.log(roles.includes("admin"));
console.log(roles.includes("edit"));true
falseThe array form fails closed. A security check that uses the string form fails open, which is the worse direction to be wrong in. The array method is covered in [the post on Array.prototype.includes()](/library/javascript-array-includes).
Choosing between includes and its neighbors
Several methods answer a nearby question, and the right pick removes the post-processing.
| You want | Use | Returns |
|---|---|---|
| Does the text contain this substring | includes() | true or false |
| Where does the substring first occur | indexOf() | index or -1 |
| Does the text begin with a prefix | startsWith() | true or false |
| Does it match a pattern | RegExp.prototype.test() | true or false |
| Every match with positions | matchAll() | iterator of match objects |
We reach for includes() whenever the question is a plain yes or no, because a boolean cannot be misread the way indexOf() can. The classic bug with indexOf() is writing if (text.indexOf("x")), which treats a match at index 0 as a miss and a missing value, -1, as a hit. includes() has no such trap.
Where includes stops being the right tool
Substring search is the wrong tool when the thing you are testing has structure. Checking whether a URL contains "/admin" also accepts "/administrators-list" and "/public/admin-guide". Parse the URL and compare the path segments instead. The same applies to file extensions, where name.includes(".js") accepts "data.json", and to email domains, where address.includes("example.com") accepts "[email protected]". In each case the boolean answers a question that sounds like the one you have and is not.
It is also the wrong tool for matching across languages and scripts without normalization, and for anything that must be case-insensitive in a locale-aware way. For repeated searches over a large body of text with many terms, a compiled regular expression or an index built once is cheaper than calling includes() for every term, since each call scans the text from the start position again.
The method is defined in the ECMAScript specification as String.prototype.includes, and MDN documents it on the String.prototype.includes page. To see how code units and normalization interact, read the normalize() page on MDN.


