JavaScript String includes() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Instance method

What You’ll Learn

String.prototype.includes() performs a case-sensitive search and returns true or false if a substring is found (same API as MDN String.prototype.includes()). Learn the optional start position, empty-string behavior, why regex is not allowed, how it compares to indexOf(), five examples, and try-it labs.

01

Kind

Instance method

02

Returns

true / false

03

Case

Sensitive

04

Regex?

No (TypeError)

05

Mutates?

No

06

Baseline

Widely available

Introduction

When you only need to know whether a string contains some text — not where it is — includes() is the clearest tool. It returns a boolean, so it fits naturally in if conditions and ternary expressions.

The search is always case-sensitive. For a case-insensitive check, lower-case both sides first. For the index of a match, use indexOf() instead. For patterns, use a regular expression API — includes() rejects regex arguments.

💡
Beginner tip

Prefer str.includes(needle) over str.indexOf(needle) !== -1 when you only need a yes/no answer — it reads more clearly.

This page is part of JavaScript String Methods. Related topics include at() and length.

Understanding the includes() Method

includes(searchString, position) scans the receiver from position (default 0) looking for an exact substring match.

  • Returns true or false — never an index.
  • Matching is case-sensitive and exact (no wildcards).
  • An empty searchString always yields true.
  • A RegExp argument throws TypeError.

📝 Syntax

General forms of String.prototype.includes:

JavaScript
str.includes(searchString)
str.includes(searchString, position)

Parameters

  • searchString — the substring to look for. Cannot be a regex. Non-regex values are coerced to strings (undefined becomes "undefined").
  • position (optional) — zero-based index where the search starts. Defaults to 0.

Return value

A boolean. Special cases:

SituationReturns
Substring foundtrue (e.g. "fox".includes("ox"))
Substring missingfalse
Empty searchStringtrue (even for "".includes(""))
Case differsfalse (e.g. "Hi".includes("hi"))
Start position past the matchfalse (search never looks before position)
searchString is a RegExpThrows TypeError

Common patterns

JavaScript
"Hello world".includes("world");           // true
"Hello world".includes("World");           // false
"Hello world".toLowerCase().includes("world"); // true

"abcdef".includes("cd", 2);                // true
"abcdef".includes("cd", 3);                // false

// Need the index? Use indexOf:
"abcdef".indexOf("cd");                    // 2

⚡ Quick Reference

GoalCode
Contains substring?str.includes(needle)
Search from an indexstr.includes(needle, start)
Case-insensitivestr.toLowerCase().includes(needle.toLowerCase())
Need the positionstr.indexOf(needle)
Pattern / regex/pattern/.test(str)

🔍 At a Glance

Four facts to remember about String.includes().

Returns
boolean

true or false

Case
sensitive

Normalize if needed

Empty needle
true

Always matches

Mutates
no

Strings stay immutable

📋 includes() vs indexOf() vs regex

includes()indexOf()RegExp.test()
Resulttrue / falseIndex or -1true / false
Best forSimple contains checksNeed the positionPatterns / flags
CaseSensitiveSensitivei flag optional
Regex argumentThrowsTreated as string patternNative

Examples Gallery

Examples follow MDN String.includes() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Ask whether a string contains another string.

Example 1 — Basic includes()

MDN demo: report whether a word appears in a sentence.

JavaScript
const sentence = "The quick brown fox jumps over the lazy dog.";
const word = "fox";

`The word "${word}" ${
  sentence.includes(word) ? "is" : "is not"
} in the sentence`;
// 'The word "fox" is in the sentence'
Try It Yourself

How It Works

includes("fox") returns true, so the ternary picks "is". Change word to something missing to see "is not".

Example 2 — Several True / False Checks

MDN-style walkthrough of common results.

JavaScript
const str = "To be, or not to be, that is the question.";

str.includes("To be");        // true
str.includes("question");     // true
str.includes("nonexistent");  // false
str.includes("To be", 1);     // false  (starts searching after "T")
str.includes("TO BE");        // false  (case-sensitive)
str.includes("");             // true
Try It Yourself

How It Works

With position 1, the leading "To be" no longer counts — the search starts at index 1 ("o be...").

📈 Practical Patterns

Case folding, start positions, and edge cases.

Example 3 — Case Sensitivity

MDN note: normalize case when you need a flexible match.

JavaScript
"Blue Whale".includes("blue");  // false

"Blue Whale".toLowerCase().includes("blue");  // true

function includesIgnoreCase(str, needle) {
  return str.toLowerCase().includes(needle.toLowerCase());
}
includesIgnoreCase("JavaScript", "SCRIPT");  // true
Try It Yourself

How It Works

Lower-casing both sides compares letters without caring about uppercase vs lowercase. For locale-heavy text, consider toLocaleLowerCase().

Example 4 — Start position

Limit the search to a suffix of the string.

JavaScript
const file = "report.final.pdf";

file.includes(".");           // true
file.includes(".", 7);        // true  (still finds ".pdf")
file.includes("report", 1);   // false (skipped the start)
file.includes("pdf", 12);     // true
file.includes("pdf", 13);     // false
Try It Yourself

How It Works

position is where searching begins — earlier characters are ignored. It does not mean “must start exactly at this index” (that is closer to startsWith / indexOf === position).

Example 5 — Empty String, Coercion, and Regex Error

Three gotchas beginners should know.

JavaScript
"abc".includes("");     // true
"".includes("");        // true

"undefined".includes(undefined);  // true  (undefined → "undefined")
"abc".includes(undefined);        // false

try {
  "abc".includes(/a/);
} catch (e) {
  e.name;  // "TypeError"
}

// Prefer includes for boolean checks:
"hello".includes("ll");           // true
"hello".indexOf("ll") !== -1;     // true (older style)
Try It Yourself

How It Works

Empty needles always match. Omitted / undefined needles become the text "undefined". Regex must use .test() instead.

🚀 Common Use Cases

  • Feature flags / keywords — detect a token in user input or config.
  • File / URL checksname.includes(".pdf") style filters.
  • Guards in conditionals — readable if (msg.includes("error")).
  • Case-insensitive filters — pair with toLowerCase().
  • Not for positions — switch to indexOf / search when you need the index.
  • Not for patterns — use regex when you need wildcards or flags.

🧠 How includes() Searches

1

Receive needle + start

Coerce searchString; default position to 0. Reject regex.

Input
2

Scan from position

Look for an exact, case-sensitive substring match.

Search
3

Found?

Empty needle counts as found; otherwise require a real match.

Decide
4

Return true or false

A boolean ready for if / ternary use — no index value.

📝 Notes

  • includes() is case-sensitive.
  • Passing a RegExp throws TypeError.
  • Empty searchString always returns true.
  • Omitting the needle (or passing undefined) searches for "undefined".
  • position only changes where the search starts — not a “must begin at” rule.
  • Use indexOf() when you need the match index.

Browser & Runtime Support

String.prototype.includes() is Baseline Widely available — supported across modern browsers since 2015.

Baseline · Widely available

String.includes()

Safe for production. Prefer includes() for boolean contains checks; use indexOf() when you need the position.

Universal Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
includes() Excellent

Bottom line: Use String.includes() for clear true/false substring checks. Remember case sensitivity, empty-string matches, and that regex arguments are not allowed.

Conclusion

String.prototype.includes() is the modern, readable way to ask “does this string contain that text?” — returning true or false with an optional start position. Keep case rules and the empty-needle quirk in mind, and reach for indexOf or regex when you need more than a boolean.

Continue with indexOf(), at(), or the String methods hub.

💡 Best Practices

✅ Do

  • Use includes() for yes/no substring checks
  • Normalize case when matches should ignore capitalization
  • Pass a start position when scanning a suffix
  • Use indexOf() when you need the match index
  • Use regex APIs for patterns and flags

❌ Don’t

  • Pass a RegExp to includes()
  • Forget that "" always matches
  • Omit the needle and expect a useful default search
  • Assume matches ignore case
  • Use includes when you actually need the index

Key Takeaways

Knowledge Unlocked

Five things to remember about String.includes()

Case-sensitive boolean substring search.

5
Core concepts
🔎 02

Case

sensitive

Rule
03

Regex

TypeError

Guard
04

vs indexOf

boolean vs index

Compare
📄 05

Mutates

no

Immutable

❓ Frequently Asked Questions

String.prototype.includes(searchString, position?) returns true if searchString is found anywhere in the string (from the optional start position), otherwise false. The search is case-sensitive.
Yes. "Blue Whale".includes("blue") is false. Normalize both sides with toLowerCase() (or toLocaleLowerCase()) when you need a case-insensitive check.
No. If searchString is a RegExp, includes() throws TypeError. Use RegExp.test() or String.match() for pattern searches.
true for any string, including the empty string. An empty search string is treated as found.
includes() returns a boolean — ideal for if checks. indexOf() returns the first index, or -1 if missing — better when you need the position.
No. Strings are immutable. includes() only reads and returns true or false.
Did you know?

Before includes(), developers wrote str.indexOf(needle) !== -1 everywhere. The boolean method was added in ES2015 to make that intent obvious — Arrays got Array.prototype.includes() for the same reason.

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful