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.
How do I check if a string contains a substring in JavaScript? Execute
let str = 'JavaScript String Contains';

console.log(str.includes('String'));
Updated: Viewed: 1396 times

Checking if a string contains a substring using the includes() method

In JavaScript, the string.include() method determines whether a string contains the given characters or not. The method was introduced in ES6 and is generally preferred for simple use cases. The include() method returns the boolean "true" if the string contains characters otherwise, it returns "false". The includes() method is case sensitive.

JavaScript include() Syntax
string.includes(term, start)

Where:
  • term: this is the string to be searched
  • start (optional): this is the position from which the search will be performed. If the parameter is not specified, then the default value is 0
Сheck if a string contains a substring using the include() method
let str = 'Hello World';

console.log(str.includes('world'));

// output: false

How to find the index of a substring in a string in JavaScript?

To find the index of a substring in JavaScript, you can use the built-in string.indexOf() method. The string.indexOf() method checks if a string contains the specified substring and returns the index of the first occurrence. The characters in the string are indexed from left to right and returns the index position of the substring starting at 0, and -1 if the substring was not found. Like the includes() method, it is also case sensitive.

JavaScript indexOf() Syntax
string.indexOf(term, start)

Where:
  • term: the string to be found
  • start (optional): the position at which the search begins in the string. The default is 0.
JavaScript String Contains Example
let str = 'JavaScript String Contains';

console.log(str.indexOf('String'));

// output: 11

How to check if a string contains a substring using regular expressions?

To test for a substring in a string using regular expressions in JavaScript, you can use the RegExp.test() method. Regular expressions give you a lot more flexibility than the previous methods where you can only check for a constant string. The downside of using regular expressions is that they are slower than checking for a substring in a string using the includes() and indexOf() methods.

Test String Contains Substring using Regular Expressions/span>
let str = 'JavaScript String Contains';

console.log(/javascript/.test(str));

// output: false

Unlike the includes(), indexOf() methods, we can do a case-insensitive search with the "i" flag:

Case Insensitive JavaScript String Contains Example
let str = 'JavaScript String Contains';

console.log(/javascript/i.test(str));

// output: true

See also