JavaScript string includes() Method

The includes method in JavaScript is utilized to determine whether a certain substring exists within a specified string. This method is case-sensitive. It yields a Boolean result, either true or false. If the string contains the provided substring, it will return true; otherwise, it will return false.

It does not alter the value of the initial string.

Syntax

The syntax outlined below illustrates the includes method:

Example

string.includes(searchValue, start);

Parameter values

The values of the parameters for this method are specified in the following manner:

searchValue: This parameter is mandatory. It represents the substring that needs to be searched for.

It is an optional argument that indicates the index at which to commence the search within the string. The default setting for this parameter is 0. If this parameter is not specified, the search will initiate from the beginning of the string, which is at index 0.

Let’s explore the includes method through a few illustrative examples.

Example1

This is a straightforward illustration to check if a particular substring exists within a specified string. In this case, we initialize a variable named str and give it the string value 'Welcome to the logic-practice.com'. Subsequently, we utilize the includes method to ascertain whether the designated substring (' to ') is included in the string.

In this instance, we are not specifying a starting point for the search. Consequently, the search process will commence from the beginning of the string.

Example

<!DOCTYPE html>
<html>
<head>
</head>

<body>
<h1> Hello world :):) </h1>
<h3> This is an example of using the JavaScript's string includes() method. </h3>
<script>
let str = "Welcome to the logic-practice.com";  
document.write(" <b> The given string is: </b>", str);
document.write("<br>");
let res = str.includes('tO');
document.write(" <b> The result is: </b> ", res); 
</script>
</body>
</html>

Output

Example2

In this instance, we are examining if the includes method is sensitive to letter case. The string provided is 'Welcome to the logic-practice.com'. Our objective is to search for the substring 'TO' within this specified string.

Even though the term 'to' exists within the provided string, the method is case-sensitive; therefore, it will yield a Boolean value of false.

Example

<!DOCTYPE html>
<html>
<head>
</head>

<body>
<h1> Hello world :):) </h1>
<h3> This is an example of using the JavaScript's string includes() method. </h3>
<p> Here, we are searching for the substring <b> 'TO' </b> in the given string. </p>
<script>
let str = "Welcome to the logic-practice.com";  
document.write(" <b> The given string is: </b>", str);
document.write("<br>");
let res = str.includes('TO');
document.write(" <b> The result is: </b> ", res); 
</script>
</body>
</html>

Output

Example3

In this demonstration, we are establishing the starting point for the search process. Consequently, the search will commence from the designated position.

Example

<!DOCTYPE html>
<html>
<head>
</head>

<body>
<h1> Hello world :):) </h1>
<h3> This is an example of using the JavaScript string includes() method. </h3>
<script>
let str = "Welcome to the logic-practice.com";  
document.write(" <b> The given string is: </b>", str);
document.write("<br>");
let res = str.includes('the', 10);
document.write(" <b> The result of str.includes('the', 10) is : </b> ", res); 
</script>
</body>
</html>

Output

Input Required

This code uses input(). Please provide values below: