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

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

By Rajanikant Hawaldar in JavaScript on Oct 10 2020
  • Harshal Limaye
    Dec, 2020 24

    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.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS