How to check whether a string contains a substring in JavaScript?

Programise
6 min readJan 15, 2023

--

There are several ways to check if a string contains a substring in JavaScript. Here are a few of the most common methods:

substring in javascript
Substring in JavaScript

1. Using the indexOf() Method:

  • The indexOf() method returns the index of the first occurrence of a specified string value in the calling string. If the specified value is not found, it returns -1.
  • You can use this method to check if a string contains a substring by checking if the returned index is greater than or equal to 0.

Code:

let str = "Hello, world!";
let substring = "world";
if (str.indexOf(substring) >= 0) {
console.log("The substring was found!");
} else {
console.log("The substring was not found.");
}

Code Explanation:

  • let str = "Hello, world!";: This line declares a variable named "str" and assigns it the value "Hello, world!" which is the string that we want to check for the substring.
  • let substring = "world";: This line declares a variable named "substring" and assigns it the value "world" which is the substring that we want to check for in the string.
  • if (str.indexOf(substring) >= 0): This line uses the indexOf() method to check if the substring is present in the string. The indexOf() method returns the index of the first occurrence of the specified substring in the string. If the substring is found, the method returns a value greater than or equal to 0. If the substring is not found, it returns -1. So this line of code checks if the index returned by the indexOf() method is greater than or equal to 0. If it is, it means that the substring is present in the string.
  • console.log("The substring was found!");: If the substring is found in the string, this line of code will be executed. It will print the message "The substring was found!" to the console.
  • else: If the substring is not found in the string, the code inside the else block will be executed.
  • console.log("The substring was not found.");: This line of code will be executed if the substring is not found in the string. It will print the message "The substring was not found." to the console.

2. Using the includes() Method:

  • The includes() method determines whether a string contains the characters of a specified string.
  • It returns true if the string contains the characters, and false if not.

Code:

let str = "Hello, world!";
let substring = "world";
if (str.includes(substring)) {
console.log("The substring was found!");
} else {
console.log("The substring was not found.");
}

Code Explanation:

  • let str = "Hello, world!";: This line declares a variable named "str" and assigns it the value "Hello, world!" which is the string that we want to check for the substring.
  • let substring = "world";: This line declares a variable named "substring" and assigns it the value "world" which is the substring that we want to check for in the string.
  • if (str.includes(substring)): This line uses the includes() method to check if the substring is present in the string. The includes() method returns a boolean value indicating whether or not the substring is present in the string. If it is present, it returns true, and if it is not present, it returns false. So this line of code checks if the substring is present by checking if the includes() method returns true.
  • console.log("The substring was found!");: If the substring is found in the string, this line of code will be executed. It will print the message "The substring was found!" to the console.
  • else: If the substring is not found in the string, the code inside the else block will be executed.
  • console.log("The substring was not found.");: This line of code will be executed if the substring is not found in the string. It will print the message "The substring was not found." to the console.

3. Using the search() Method:

  • The search() method searches a string for a specified value and returns the position of the match.
  • If no match is found, it returns -1.

Code:

let str = "Hello, world!";
let substring = "world";
if (str.search(substring) >= 0) {
console.log("The substring was found!");
} else {
console.log("The substring was not found.");
}

Code Explanation:

  • let str = "Hello, world!";: This line declares a variable named "str" and assigns it the value "Hello, world!" which is the string that we want to check for the substring.
  • let substring = "world";: This line declares a variable named "substring" and assigns it the value "world" which is the substring that we want to check for in the string.
  • if (str.search(substring) >= 0): This line uses the search() method to check if the substring is present in the string. The search() method searches a string for a specified value and returns the position of the match. If no match is found, it returns -1. So this line of code checks if the substring is present by checking if the search() method returns a value greater than or equal to 0.
  • console.log("The substring was found!");: If the substring is found in the string, this line of code will be executed. It will print the message "The substring was found!" to the console.
  • else: If the substring is not found in the string, the code inside the else block will be executed.
  • console.log("The substring was not found.");: This line of code will be executed if the substring is not found in the string. It will print the message "The substring was not found." to the console.

4. Using the match() Method:

  • The match() method searches a string for a match against a regular expression and returns the matches, as an Array object.
  • If no match is found, it returns null.

Code:

let str = "Hello, world!";
let substring = "world";
if (str.match(substring)) {
console.log("The substring was found!");
} else {
console.log("The substring was not found.");
}

Code Explanation:

  • let str = "Hello, world!";: This line declares a variable named "str" and assigns it the value "Hello, world!" which is the string that we want to check for the substring.
  • let substring = "world";: This line declares a variable named "substring" and assigns it the value "world" which is the substring that we want to check for in the string.
  • if (str.match(substring)): This line uses the match() method to check if the substring is present in the string. The match() method searches a string for a match against a regular expression and returns the matches, as an Array object. If no match is found, it returns null. So this line of code checks if the substring is present by checking if the match() method returns a non-null value.
  • console.log("The substring was found!");: If the substring is found in the string, this line of code will be executed. It will print the message "The substring was found!" to the console.
  • else: If the substring is not found in the string, the code inside the else block will be executed.
  • console.log("The substring was not found.");: This line of code will be executed if the substring is not found in the string. It will print the message "The substring was not found." to the console.

Note: It is important to note that match() method will not work as expected in this case because it is looking for a match using regular expression, if we want to use match() method we should use RegEx as argument like str.match(/world/)

Conclusion:

In conclusion, there are several ways to check if a string contains a substring in JavaScript. The most common methods include using the indexOf(), includes(), search(), and match() methods. Each of these methods has its own set of advantages and disadvantages, and the best method to use will depend on your specific use case.

indexOf() method returns the index of the first occurrence of a specified string value in the calling string. If the specified value is not found, it returns -1. includes() method determines whether a string contains the characters of a specified string and returns true if the string contains the characters, and false if not. search() method searches a string for a specified value and returns the position of the match. If no match is found, it returns -1. match() method searches a string for a match against a regular expression and returns the matches, as an Array object. If no match is found, it returns null.

It is also important to note that all above methods are case sensitive.

--

--