Problem Statement
check if string contains specific substring using JavaScript
Solution
to check if a string contains a given substring using JavaScript, we can use String includes() method. call includes()
method on the string, and pass the substring as argument. includes()
returns true
if the string contains the substring, or false
if the string does not contain the substring
Program
1. given string is str
and substring is substr
. check if string str
contains substr
str = "hello world";
substr = "wor";
if ( str.includes(substr) ) {
console.log('string contains specified substring');
} else {
console.log('string does not contain specified substring');
}