Checking if String Contains a Substring in JavaScript

To check if a string contains a substring in JavaScript, use the string.includes(term, start) method, which checks if the string contains the substring. The first parameter is the string to search for, and the second optional parameter specifies the position from which to search. The string.includes() method is case-sensitive. Also, you can check if the string contains a substring by using a string.indexOf(term, start), which returns the index of the substring within the string instead of the boolean value returned by the string.includes(). In this JavaScript String Contains Substring example, we use the string.includes() to check if the substring is in the string. You can see more examples of searching for substrings in strings below, including using JavaScript Regular Expressions. Click Execute to run the JavaScript String Contains Substring Example online and see the result.
Checking if String Contains a Substring in JavaScript Execute
let str = 'JavaScript String Contains';

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

What is JavaScript?

JavaScript is a scripting language used in browsers to build powerful web applications and build high-performance web servers (NodeJs). JavaScript is the primary programming language for the web. Currently, 98% of websites use JavaScript to program the behavior of web pages, and all major web browsers support it. JavaScript is also widely used on the server side (Node.js), allowing developers to use JavaScript to write command-line tools and run server-side scripts.

What is String in JavaScript?

A JavaScript string is a sequence of one or more characters, which can be letters, numbers, or symbols. JavaScript strings can contain Unicode characters encoded using the \uXXXX syntax. Strings in JavaScript are primitive and immutable data types, meaning they cannot be changed once created. JavaScript has a wide range of methods for working with strings. You can split, concatenate, trim and reverse strings; find the length of a string and check if it starts or ends with another string, and much more.

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

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

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

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

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

// output: false

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

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, or -1. Indexing in JavaScript starts at 0. The string.indexOf() method is case-sensitive.

JavaScript string.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. default value 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 much more flexibility than the previous methods where you can only check for a constant string. The disadvantage of regular expressions is that they are slower than checking for a substring in a string using the string.includes() and string.indexOf() methods.

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

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

// output: false

Unlike the string.includes() and string.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

How to check if a string contains a number in JavaScript?

To check if a string contains numbers in JavaScript, you can use regular expressions. To search for a specific number in a string, you can use the string.includes() or string.indexOf() methods.

JavaScript String Contains Numbers Example
let str = 'JavaScript ES6 was introduced in 2015.';

console.log(/\d/.test(str));

// output: true

How to check if a string contains a word in JavaScript?

To search for a whole word in a string, you can use regular expressions by enclosing the word in special characters \b (word boundary). Unlike searching for a substring in a string, the word must be separated from the rest of the text by a space, a newline, or the beginning of a line.

JavaScript Search Contains Whole Word Example
let str = 'JavaScript ES6 was introduced in 2015.';

console.log(/\bes6\b/i.test(str));

// output: true

How to check if a string contains a character in JavaScript?

To check if a string contains a particular character in JavaScript, you can use the string.includes() or string.indexOf() methods, or regular expressions, as you would when searching for a substring in a string.

The fastest way to check if a string contains the substring

The string.includes() method is the fastest way to check if a string contains another substring. You should prefer this method over other methods unless you need case-sensitive comparisons and complex search patterns.

See also