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,
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:
Case Insesnitive
To make the check case insensitive add .toUpperCase()
. Read more about case-insensitive string comparison in JavaSCript.