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
:
JS
Copy code
And here's an example of using includes
:
JS
Copy code