Skip to main content

includes() JavaScript String Method – How to Check If a String Includes a Substring

Whenever you use includes() on a string, the method does the following:

  1. It checks if the calling string includes the method's first argument.
  2. It returns true if the string includes the argument. Otherwise, it returns false.
note
  • A calling string is a string on which you used includes(). So, in "Hello, world!".includes("world"), "Hello, world!" is the calling string.
  • includes() is sometimes written as String.prototype.includes() because it is a method of the String object's prototype property.

Syntax of the includes() Method

includes() accepts two arguments. Here is the syntax:

callingString.includes(valueToFind, startIndex);
note

The includes() method is case-sensitive. You can use the search() method to do a case-sensitive search.

Argument 1: valueToFind

A valueToFind is the first argument accepted by the includes() method. It defines the value you wish to find in the calling string.

Example 1: Check if a given string includes "day"

"SunDay, Tuesday, and Friday are good Days".includes("day");

// The invocation above will return: true

Try Editing It

Example 2: Check if a given string includes "DAY"

"SunDay, Tuesday, and Friday are good Days".includes("DAY");

// The invocation above will return: false

Try Editing It

The snippet above returned false because the includes() method is case-sensitive. You can use the search() method to do a case-sensitive search.

Example 3: Check if a given string includes 3

"Day1, day-3, and day 6 are good Days".includes(3);

// The invocation above will return: true

Try Editing It

Argument 2: startIndex

The startIndex argument is optional. It specifies the index position where you want the computer to start searching for the valueToFind argument.

note

The startIndex argument's default value is 0. Therefore, if omitted, the search will begin at index 0.

Example 1: From the 13th index position, check if a given string includes "day"

"SunDay, Tuesday, and Friday are good Days".includes("day", 13);

// The invocation above will return: true

Try Editing It

Example 2: From the 1st index position, check if a given string includes "Sun"

"SunDay, Tuesday, and Friday are good Days".includes("Sun", 1);

// The invocation above will return: false

Try Editing It

tip

Overview

includes() checks if its calling string includes the method's first argument.

Tweet this article