Skip to content
jshacks.dev
Fundamentals

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.

7 min read
Summarize in AI
Flat illustration of an editor window full of code, with the JavaScript, Vite, npm and Node.js logos.

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.

ArgumentMeaningDefault
searchStringThe text to look for. Anything that is not a string is converted to onenone
positionThe index to start looking from0

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.

JavaScript
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));
Text
true
false
true
false

The 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.

JavaScript
const word = "banana";

console.log(word.includes("ban", -5));
console.log(word.includes("ban", NaN));
console.log(word.includes("a", 100));
Text
true
true
false

The 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.

JavaScript
const word = "banana";

console.log(word.includes("ana", 1));
console.log(word.includes("ana", 3));
console.log(word.includes("ana", 4));
Text
true
true
false

From 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.

JavaScript
console.log("abc".includes(""));
console.log("".includes(""));
console.log("abc".includes("", 99));
Text
true
true
true

This 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".

JavaScript
console.log("abc".includes());
console.log("value: undefined".includes());
console.log("id 42".includes(42));
console.log("a,b".includes(["a", "b"]));
Text
false
true
true
true

The 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.

JavaScript
try {
  "abc".includes(/b/);
} catch (err) {
  console.log(err.name);
  console.log(err.message);
}
Text
TypeError
First argument to String.prototype.includes must not be a regular expression

If 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.

JavaScript
const haystack = "Release Notes";
const needle = "NOTES";

console.log(haystack.includes(needle));
console.log(haystack.toLowerCase().includes(needle.toLowerCase()));
Text
false
true

Lowercasing 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.

JavaScript
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"));
Text
false
true
4
5
true

The 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.

JavaScript
const face = "\u{1F600}";

console.log(face.length);
console.log(face.includes("\ud83d"));
console.log([...face].length);
Text
2
true
1

A 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.

JavaScript
const allowed = "admin,editor";

console.log(allowed.includes("admin"));
console.log(allowed.includes("edit"));
console.log(allowed.includes("min,ed"));
Text
true
true
true

The 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.

JavaScript
const roles = "admin,editor".split(",");

console.log(roles.includes("admin"));
console.log(roles.includes("edit"));
Text
true
false

The 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 wantUseReturns
Does the text contain this substringincludes()true or false
Where does the substring first occurindexOf()index or -1
Does the text begin with a prefixstartsWith()true or false
Does it match a patternRegExp.prototype.test()true or false
Every match with positionsmatchAll()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.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.