Checking for Substring with includes() Method

JavaScript string.includes() method can be used to check whether a string contains a specified substring. It returns true if the substring is present. This method is case-sensitive. 

Syntax:

string.includes(searchvalue, start)

Example: This example uses the includes() method of JavaScript to check for the substring in a string.

Javascript
let str = "Hello there! Welcome to w3wiki";
let flag = str.includes("Geeks");
console.log(flag);

Output
true



Explanation:

The code checks if the string “Hello there! Welcome to w3wiki” contains the substring “Geeks”. It assigns the result of this check to the variable `flag` and logs it to the console.

How to check whether a string contains a substring in JavaScript ?

A substring is a portion of a string. It represents a sequence of characters extracted from a larger string. A substring can be as short as a single character or extend to multiple characters.

There are several approaches to check whether a string contains a substring or not:

Table of Content

  • Checking for Substring with includes() Method
  • Checking for Substring with test() Method
  • Checking for Substring with match() Method
  • Checking for Substring with indexOf() Method
  • Checking for Substring with search() Method

Similar Reads

Checking for Substring with includes() Method

JavaScript string.includes() method can be used to check whether a string contains a specified substring. It returns true if the substring is present. This method is case-sensitive....

Checking for Substring with test() Method

In this method, we will use the test() method to test the substring in the string. This method returns true if the substring found else returns false....

Checking for Substring with match() Method

In this approach, JavaScript string.match() method with a regular expression checks if a substring exists in the string, returning null if not found or an array of matches if found....

Checking for Substring with indexOf() Method

JavaScript indexOf() method returns the position of a substring in a string or -1 if not found, allowing us to check the substring’s existence....

Checking for Substring with search() Method

In this approach, the search() method is called on the str string with a regular expression object created from the substring value using the new RegExp(substring). It searches for the first occurrence of the substring within the string. By checking if the result is not equal to -1, we determine if the substring exists in the string. The boolean result is stored in the containsSubstring variable and printed to the console....

Checking for Substring with slice() Method

In this method, we’ll use the slice() method to extract substrings from the main string and compare them with the search value. This method iterates through the main string, extracts a substring of the same length as the search value at each position, and checks for equality. If a match is found, it returns true....