Tutorials Tonight
Follow Us

JavaScript String includes method to check substring in a string


In this tutorial, you will get to know how to check if the string includes substring in javascript using the includes() string method.

In many programming problems, you would like to check the occurrence of a string or character in another string or you want to count a number of occurrences of a word in a paragraph.

In such cases use javascript includes method for strings.

JavaScipt string includes

String includes Method JavaScript

The includes() string method is used to search if a string contains another string within it or not.

The method is applied on a string and the string to be searched is passed as an argument in the method, if the calling string contains the searched string then it returns true else return false.

Note: The includes() method is case-sensitive.

const quote = "Learning to code is learning to create and innovate";
const search = "innovate";
console.log(quote.includes(search)); // true
▶ Try It

Syntax of includes string method

The syntax of includes() string method is:

string.includes(search_string [ ,start_position])

The method accepts 2 arguments:

  1. search_string - This is the string to be searched
  2. start_position (optional) - It is the position within the string from where the method starts searching

Let's search a string from the sentence with start position value:

Example: Using includes with start position

const quote = "Learning to code is learning to create and innovate";
const search = "create";
console.log(quote.includes(search, 10));
console.log(quote.includes(search, 40));
▶ Try It

In the above example, the search for the string ("create") starts from index 10 and 40 in two different lines, in first-line method starts searching from index 10 and finds the search string at index value 31 and in the second-line, it starts from index 40 and doesn't find the search string hence return false.

Case-sensitivity

The includes() method is case sensitive, which means it treats string with the same characters but the different case as different.

Check out the following example:

const string = "Big Blue Ocean";
console.log(string.includes("big"));
console.log(string.includes("BIG"));
console.log(string.includes("Big")); // case-sensitive only this matches
▶ Try It

includes String Method Example

Here are some random string searching:

const quote = "Learning to code is learning to create and innovate";
console.log(quote.includes("Learning to"));
console.log(quote.includes("code is"));
console.log(quote.includes("create and"));
console.log(quote.includes("learning is"));
console.log(quote.includes("ocean"));

How to check if the string includes substring javascript?

Simply use the includes() method to the string and pass the search string as the first argument. If you also want to find an index of the search string within the string then use the indexOf Method method.

Example 1

let case1 = "Plant more trees. Make the planet green.".includes("tree"); // return true
let case2 = "Plant more trees. Make the planet green.".includes("trees"); // return true
let case3 = "Plant more trees. Make the planet green.".includes("Trees"); // return false
console.log(case1, case2, case3);

Example 2

let string = "Hello World!";
if (string.includes("World")) {
  console.log("Yes 'World' exists in string.");
}

Alternative to includes Method: indexOf Method

The indexOf method is used to get the index of the search string within a string. It returns the index value at which the string is present.

If the string is not present then it returns -1. You can use this to find if the string is present or not.

Example

const quote = "Learning to code is learning to create and innovate";
const search = "innovate";
if (quote.indexOf(search) > -1) {
  console.log("true! Search string is present in given string.");
}
else {
  console.log("false! Search string is not present in given string.");
}

Points to remember:

  1. To find a string within a given string use includes method.
  2. There is an alternate method to find a string within a string: indexOf method.
Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.