How to check if a string contains a substring in Javascript

The most common (and supported) way to check if a string contains a substring is to use indexOf (opens new window). You can also use includes (opens new window) but it's only supported on newer browsers that support ECMAScript 6 (ES6). Both do a case-sensitive search so if you're looking for a case-insensitive search, you would want to run toLowerCase (opens new window) on both the input and search param.

Here is an example of using indexOf:

Copy code
      
    


And here's an example of using includes:

Copy code
      
    

Join our community

Your sign-up could not be saved. Please try again.
Your sign-up was successful.

Sign-up and get tips to becoming a software engineer.