Check if String Contains Substring JavaScript

To check whether string contains subString javascript; Through this tutorial, i am going show you how to check whether a string contains substring characters in JavaScript with the help of include() method.

JavaScript String Contains SubString

JavaScript includes() method is used to check whether a string contains the given characters(substring).

Syntax of JavaScript Include() Method

The basic syntax of the include() method; as shown below:

string.includes(searchvalue, start);

Parameters of JavaScript Include() 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.

Example 1 – To check whether a string contains a substring or character in JavaScript using the include() method

See the following example to check string contains substring in javaScript using include() method; as shown below:

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

str.includes("contains");

//The output of this

  true

Output:

Output: true

Example 2 – To check whether a string contains a substring or character in JavaScript using the include() method

See the second following example to check string contains substring in javaScript using include() method; as shown below:

The example-2 is linked to StackOverflow.com

Example 3 – To check whether a string contains a substring or character in JavaScript using the include() method

See the third following example to check string contains substring in javaScript using include() method; as shown below:

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

 str.includes("contains", 5);

 //The output of this

 true

Output:

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.

More JavaScript Tutorials

Leave a Comment