[Javascript] What is the best way to check whether a string contains a substring?


I want to check whether substring is present in a given string? What is the best way to do that in Javascript?

1 Answer

3 years ago by

You can use either indexOf() or includes to check if a substring is present in a given string.

let str = 'this is a test message';

let substring = 'test'

console.log(str.includes('test'));

console.log(str.indexOf('test') > 0);

Run here https://onecompiler.com/javascript/3xns8xfgc

3 years ago by Meera