Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

includes

String.prototype.includes() performs a case-sensitive substring search and reports whether the requested text occurs in a string. It returns a boolean and accepts an optional starting position.

How it works

Call includes() on a string with the text to find, optionally providing the zero-based position where searching should start. It returns a boolean and compares characters case-sensitively; the starting position is converted and constrained to the string’s bounds.

Syntax

includes(searchString)
includes(searchString, position)

Parameters

NameTypeDescription
searchStringstringThe string to look for within the receiver. It must not be a regular expression; other values are converted to strings.
positionnumberThe zero-based position at which to begin searching. It defaults to 0.

Returns

A boolean: true if the search string occurs at or after the starting position; otherwise false.

Key points

  • It checks whether the receiver contains the requested substring using case-sensitive matching.
  • The result is a boolean: true when the substring is found and false otherwise.
  • An optional position lets you skip earlier characters when starting the search.
  • If no starting position is supplied, searching begins at index zero.
  • An empty search string returns true, including when the source string itself is empty.
  • Search arguments other than regular expressions are converted to strings before matching.

Common mistakes

  • The search value cannot be a regular expression; passing one throws a TypeError.
  • Omitting the search argument or explicitly passing undefined searches for the literal string "undefined".
  • Different letter casing does not match; normalize both strings first when a case-insensitive check is needed.

Example: Checking whether a string contains text

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

console.log(text.includes("question")); // true
console.log(text.includes("TO BE")); // false
console.log(text.includes("be", 10)); // false
console.log(text.includes("")); // true

The method performs a case-sensitive substring check and can begin searching at the supplied position.

When to use it

Check whether user input contains a required token

Validate input

Keep strings that contain a specific word or phrase

Filter text

Choose a branch based on substring presence

Guard conditional logic

Ignore a prefix by starting at a later position

Search from an offset

Interview questions

QWhat does String.prototype.includes() return?

String.prototype.includes() returns a boolean indicating whether a string contains the supplied substring. The comparison is case-sensitive, and an empty search string is considered present.

Follow-up: What happens when the search argument is a regular expression? It throws a TypeError rather than performing a pattern search.

QHow does the position argument affect the search?

The optional position argument specifies where the search begins and defaults to zero. It is converted to an integer-like value and constrained to the string’s valid range.

Follow-up: Does a position beyond the string length match an empty search string? Yes, because the empty string is found at the effective endpoint.

QHow are non-string search arguments handled?

The method coerces non-regexp search values to strings. If the argument is omitted or is undefined, the searched value becomes the string "undefined".

Follow-up: How can a case-insensitive search be implemented? Convert both the source and search strings to the same case before calling includes().

  • String.prototype.endsWith()
  • String.prototype.indexOf()
  • String.prototype.match()
  • String.prototype.startsWith()

Source: String.prototype.includes() - JavaScript | MDN. Checked 2026-10-15.