How do I check if a string contains a substring in JavaScript?

To check if a string contains a substring in JavaScript, you can use the string.includes(term, start) method, which determines if the string contains the given characters or not. The first parameter is the string to be searched. The second parameter is optional and specifies the position from which the search will be performed. The default value is 0. The method include() returns "true" if the string contains the specified characters, or "false" otherwise. The method is case-sensitive. You can also use the string.indexOf(term, start) method, but the only difference from includes() is the return value (the method returns the index location of the substring, or -1 if there is none). In this String Containing a Substring in the JavaScript example, we use the includes() method. Click Execute to run the JavaScript String Contains Example online and see the result.
Loading...