How to check if string contains another substring

The easiest way to check if string contains another substring is by using indexOf() Javascript method.

The indexOf() method returns the position of the first occurrence of the specified value in a string. If the specified value is not found in the string then indexOf() returns -1.

To Check the substring position in the string using Javascript substring use the below code

To check if string contains another substring

Browsers Supported:

  • IE
  • Firefox
  • Chrome
  • Safari

Another easiest way to achieve the same functionality is by using string.includes which have been added in the latest version of ES6.

Syntax for string.includes

Example of string.includes

*Note: This may require ES6 shim in older browsers.

You may also like...

Leave a Reply

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