1
Reply

How to check whether a string contains a substring in JavaScript?

How to check whether a string contains a substring in JavaScript?

    ES6 way:
    You could use includes method to perform this check.

    1. 'hello javascript'.includes('javascript') // output: true

    Before ES6:
    If you don’t wish to use includes you can go with good old indexOf method.

    1. '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.