Signup/Sign In

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

Answer: Using indexOf() method

JavaScript offers several methods to find out whether a string consists of a particular substring or not. In this lesson, we are going to discuss three methods to check whether a string contains a substring or not, these methods are:

  • indexOf() method
  • includes() method
  • search() method

Using indexOf() method

The indexOf() method checks whether a string consists of a substring. This method returns the index value or position of the first occurrence of a specified value within the given string.

This method returns -1, if the substring we are looking for is not present within the given string.

Example: Using indexOf() method

In this example, we have used the indexOf() method to check whether the substring day is present within the given string or not.

<!DOCTYPE html>
<html>
<head>
	<title>Check whether a string contains a substring</title>
</head>
<body>
<script>
	var str = "Hello! Have a nice day"    
    var index = str.indexOf("day");    
    if(index !== -1){
        console.log("Substring is present.");
    } else{
        console.log("Substring not present");
    }
</script>
</body>
</html>

Output


Substring is present.

Using includes() method

The includes() method was introduced in ECMAScript 2015. This method is used to check whether the string consists of the substring or not. This method returns true if the substring is present within the given string otherwise it returns false.

Here the string is the given string and the substring is the word or group of characters we are looking for within the given string.

Example: Using includes() method

In the given example, we have used the includes() method to check whether the substring nice is present within the given string or not.

<!DOCTYPE html>
<html>
<head>
	<title>Check whether a string contains a substring</title>
</head>
<body>
<script>
	var str = "Hello! Have a nice day"   
    if(str.includes("nice")){
        console.log("Substring is present");
    } else{
        console.log("Substring is not present");
    }
</script>
</body>
</html>

Output


Substring is present

Using search() method

We can also check whether the substring exists within the given string using the search() method. This method searches for the specified value within the given string and returns the position of the matched string. The search value can be a string or a regular expression.

This method returns -1, if the value we are looking for is not present within the given string.

Example: Using search() method

In this example, we have used the search() method to check whether the substring Hello is present within the given string or not.

<!DOCTYPE html>
<html>
<head>
	<title>Check whether a string contains a substring</title>
</head>
<body>
<script>
	var str = "Hello! Have a nice day"   
   var index = str.search(/Hello/i);  
    if(index !== -1){
        console.log("Substring is present");
    } else{
        console.log("Substring is not present");
    }
</script>
</body>
</html>

Output


Substring is present

Conclusion

JavaScript offers various built-in methods to perform several various operations. There is also some built-in function offered by JS to check whether a string consists of the substring or not. Here, we have discussed three methods through which we can hack whether the string consists of the substring or not. These methods are indexOf(), includes(), and search() method.



About the author: