JavaScript String includes() Method



The JavaScript String includes() method searches for the part of the string and determines whether the specified string is found in the original string. It returns a boolean value 'true' if the string is found in the current string, otherwise, it will return 'false'.

This is a case-senstive method, which treats strings "hi" and "Hi" as two different string values. It throws a 'TypeError' exception; if the the search string is a regex (regular expression).

Syntax

Following is the syntax of JavaScript String includes() method −

includes(searchString, position)

Parameters

This method accepts two parameters: 'searchString' and 'position', which are described below −

  • searchString − The string to be searched.
  • position − The position where the search is begin.

Return value

This method returns 'true' if specified string is found in the original string; 'false' otherwise.

Example 1

If the searchString is found in the original string, this method will return 'true'.

In the following example, we are using the String JavaScript includes() method to determine whether the searchString "Tutorials Point" is present in the original string "Welcome to Tutorials Point".

<html>
<head>
<title>JavaScript String includes() Method</title>
</head>
<body>
<script>
   const str = "Welcome to Tutorials Point";
   const searchString = "Tutorials Point";
   document.write("Original string: ", str);
   document.write("<br>Search string: ", searchString);
   document.write("<br>Is string '", searchString,"' found in '", str, "' or not? ", str.includes(searchString));
</script>    
</body>
</html>

Output

The above program returns 'true'.

Original string: Welcome to Tutorials Point
Search string: Tutorials Point
Is string 'Tutorials Point' found in 'Welcome to Tutorials Point' or not? true

Example 2

If we pass both searchString and position parameters to this method, it starts searching the searchString starting at the specified position.

Following is another example of the JavaScript String includes() method. In this example, we search the searchString 'text' within the string 'Hypertext Markup Language' at starting position 12.

<html>
<head>
<title>JavaScript String includes() Method</title>
</head>
<body>
<script>
   const str = "Hypertext Markup Language";
   const searchString = "text";
   let position = 12;
   document.write("Original string: ", str);
   document.write("<br>Search string: ", searchString);
   document.write("<br>Position: ", position);
   document.write("<br>Is string '", searchString,"' found in '", str, "' or not? ", str.includes(searchString, position));
</script>    
</body>
</html>

Output

After executing the above program, it returns 'false'.

Original string: Hypertext Markup Language
Search string: text
Position: 12
Is string 'text' found in 'Hypertext Markup Language' or not? false

Example 3

As we had discussed earlier, it is a case-sensitive method, so it will treat "hi" and "Hi" as two different values. For example: "hi how are you".includes("Hi") method returns 'false'.

<html>
<head>
<title>JavaScript String includes() Method</title>
</head>
<body>
<script>
   const str = "hi how are you";
   const searchString = "Hi";
   document.write("Original string: ", str);
   document.write("<br>Search string: ", searchString);
   document.write("<br>Is string '", searchString,"' found in '", str, "' or not? ", str.includes(searchString));
</script>    
</body>
</html>

Output

The above program returns 'false'.

Original string: hi how are you
Search string: Hi
Is string 'Hi' found in 'hi how are you' or not? false

Example 4

If the searchString is a regex (regular expression), this method will throw a 'TypeError' exception.

<html>
<head>
<title>JavaScript String includes() Method</title>
</head>
<body>
<script>
   const str = "hi how are you";
   const searchString = /\w+/;
   document.write("Original string: ", str);
   document.write("<br>Search string: ", searchString);
   try {
      document.write(str.includes(searchString));
   } catch (error) {
      document.write("<br>", error);
   }
</script>    
</body>
</html>

Output

Once the above program is executed, it will throw a 'TypeError' exception.

Original string: hi how are you
Search string: /\w+/
TypeError: First argument to String.prototype.includes must not be a regular expression
Advertisements