How to check whether a string contains a substring in JavaScript?
ES6 way:You could use includes method to perform this check.
'hello javascript'.includes('javascript') // output: true
Before ES6:If you don’t wish to use includes you can go with good old indexOf method.
'hello javascript'.indexOf('javascript') !== -1 // output: true
indexOf will return starting index of the substring, if it is found. If the substring is missing from string, it’ll return -1.