How to check String contains a Substring in JavaScript?

There are two popular ways to check string contains a substring in JavaScript: includes() and indexOf().

The includes() method

The ECMAScript 6 specification has String.prototype.includes function. Example,
javascript includes function
String.prototype.includes is case-sensitive and is not supported by Internet Explorer without a polyfill.

The indexOf() method

In ECMAScript 5 or older environments, use String.prototype.indexOf. It returns -1 when a substring cannot be found:
javascript indexOf function

Case Insesnitive

To make the check case insensitive add .toUpperCase()
case insensitive indexof. Read more about case-insensitive string comparison in JavaSCript.

Leave a Comment