Example code to find out if a string contains a substring in JavaScript.

It can be achieved through ECMAScript 6 introduced String.prototype.includes

const string = "my seo infotech";
const substring = "seo";

console.log(string.includes(substring));

in ECMAScript 5 or older environments use String.prototype.indexOf, which returns -1 when a substring does not exist.

const string = "my seo infotech";
const substring = "seo";

console.log(string.indexOf(substring) !== -1);

Similar Posts