How to check whether a string contains a substring in JavaScript?

Created by Fabien MénagerLinked to 59.9m issues across 143 teams


ECMAScript 6 introduced String.prototype.includes, which is case-sensitive and not supported by Internet Explorer without a polyfill. For ECMAScript 5 or older environments, String.prototype.indexOf should be used instead, which returns -1 when a substring cannot be found. For example:

const string = "foo"; const substring = "oo"; console.log(string.includes(substring)); // true


var string = "foo"; var substring = "oo"; console.log(string.indexOf(substring) !== -1); // true