Javascript

How to check if a string contains a substring in Javascript

Checking if a string contains a substring can be tricky sometimes but that’s why we are writing this article to show you that how you can easily check if a string in Javascript contains a substring or not.

Also Read: Check if Array includes a Value in Javascript

There are many methods to check if a string contains a substring in Javascript but the easiest one is to use the include method that is introduced in Javascript in ES6. The includes() method determines whether one string may be found within another string, returning true or false as appropriate. Here is the working example where we check if a string includes a substring or not.

var string = "foo",
    substring = "oo";

console.log(string.includes(substring));
// Output
true

So, this is all about checking if a string contains a substring or not in Javascript. Thanks for reading.

Leave a Comment