String Contains JavaScript | String includes() Method

Overview

In this tutorial, you will learn how to check whether a string contains a substring/character in JavaScript using the include() method or check if the string contains a substring

In this tutorial, we will take and show you several examples of how to check whether a string contains a character or substrings in javascript using the include method.

Before we take several examples of include method(), we must know about the javascript include() method syntax, how it works, how to pass string or substring in it, etc.

JavaScript includes()

JavaScript includes() method check whether a string contains the given characters(substring) within is exist or not.

When you use this method, Please remember some points

When you use this method, it will returns true in result if the string contains the characters(substring), otherwise, it will returns false in result.

Basically, the javaScritp includes() method is case sensitive. For example, it will treat the Uppercase characters and Lowercase characters differently.

Syntax

JavaScript include() method basic syntax is:

string.includes(searchvalue, start);

Parameters of this method

  • Search Value: This is the string that will contain the search.
  • Start: This is where the search will be processed

If the string has a substring, JavaScript provides several methods to perform the check. Learn the easy way to find a given substring or character in a string.

Examples for check whether a string contains a substring/character in JavaScript using the include() method.

Example-1

Let’s take first example, we have a string like “This is a tutsmake.com and this tutorial contains javascript include() method examples”, you want to find out in this string “contains” character. So let’s see the below example for that:

var string = "This is a tutsmake.com and this tutorial contains javascript include() method examples."

str.includes("contains");

//The output of this

  true

Demonstration: The include() method finds the “contains” character in whole string, it will return a true.

Below the source code provided for this above example is

<html>
<head>
<title>How to check whether a string contains a substring in JavaScript?</title>
</head>
<body>

<p id="show"></p> 

<script> 
  
    var str = "This is a tutsmake.com and this tutorial contains javascript include() method examples. To check whether a string contains a substring in JavaScript?"; 

    var result = str.includes("contains"); 

    document.getElementById("show").innerHTML = result; 
  
</script> 

</body>
</html>


Output: true

Example – 2

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

The example-2 is linked to StackOverflow

Example-3

Let’s take the third example that accepts an optional parameter for searching the substring or character in the given string.

javaScript includes() method also accepts an optional parameter, an integer which indicates the position where to start searching for:

 var string = "This tutorial contains javascript include() method examples."

 str.includes("contains", 5);

 //The output of this

 true

Below the source code provided for this above example is

<html>
<head>
<title>How to check whether a string contains a substring in JavaScript?</title>
</head>
<body>

<p id="show"></p> 

<script> 
  
    var str = "This tutorial contains javascript include() method examples. To check whether a string contains a substring in JavaScript?"; 

    var result = str.includes('tutorial', 5); 

    document.getElementById("show").innerHTML = result; 
  
</script> 

</body>
</html>


Output: true

Conclusion

In this JavaScript tutorial – you have learned the easy way to find/check a whether given substring or character in a string.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *