Check if a String contains a Substring in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
4 min

banner

# Check if a String contains a Substring with String.includes()

Use the String.includes() method to check if a string contains a substring.

The String.includes() method returns true if the substring is contained in the string, otherwise false is returned.

index.js
const string = 'hello world'; const substring = 'hello'; console.log(string.includes(substring)); // ๐Ÿ‘‰๏ธ true if (string.includes(substring)) { // ๐Ÿ‘‰๏ธ substring is contained in the string }

check if string contains substring

The code for this article is available on GitHub

We used the String.includes() method to check if a substring is contained in a string.

If the substring is contained in the string, the String.includes() method returns true, otherwise it returns false.

The String.includes() method is case-sensitive. To do a case-insensitive check if a substring is contained in a string, convert both strings to lowercase.

index.js
const string = 'HELLO world'; const substring = 'hello'; // ๐Ÿ‘‡๏ธ true console.log( string.toLowerCase().includes(substring.toLowerCase()) ); if (string.toLowerCase().includes(substring.toLowerCase())) { // ๐Ÿ‘‰๏ธ substring is contained in the string }

check if string contains substring case insensitive

# Check if String Doesn't contain a Substring in JavaScript

Use the logical NOT (!) operator to negate the call to the includes() method to check if a string doesn't contain a substring.

The expression will return true if the string doesn't contain the substring and false otherwise.

index.js
const str = 'hello world'; if (!str.includes('bye')) { console.log('โœ… string does not include substring'); } else { console.log('โ›”๏ธ string includes substring'); }

check if string does not contain substring

The code for this article is available on GitHub

We used the logical NOT (!) operator to negate the call to the String.includes() method.

In the example, we check if the value bye is contained in the string hello world.

index.js
const str = 'hello world'; console.log(str.includes('bye')); // ๐Ÿ‘‰๏ธ false console.log(!str.includes('bye')); // ๐Ÿ‘‰๏ธ true

If we use the includes() method without the logical NOT (!) operator, it returns true if the value is contained in the string and false otherwise.

By negating the return value of the includes() method, the expression returns true if the value is not contained in the string.

If you need to check if a string doesn't contain a substring in a case-insensitive manner, convert both strings to lowercase.

index.js
function notContains(string, substring) { return !str.toLowerCase().includes(substring.toLowerCase()); } const str = 'hello world'; console.log(notContains(str, 'BYE')); // ๐Ÿ‘‰๏ธ true console.log(notContains(str, 'HEL')); // ๐Ÿ‘‰๏ธ false

We used the String.toLowerCase() method to convert both strings to lowercase before checking if the string doesn't contain the substring.

Here are some more examples of using the logical NOT (!) operator.

index.js
console.log(!true); // ๐Ÿ‘‰๏ธ false console.log(!false); // ๐Ÿ‘‰๏ธ true console.log(!'hello'); // ๐Ÿ‘‰๏ธ false console.log(!''); // ๐Ÿ‘‰๏ธ true console.log(!null); // ๐Ÿ‘‰๏ธ true

You can imagine that the logical NOT (!) operator first converts the value to a boolean and then flips the value.

When you negate a falsy value, the operator returns true, in all other cases it returns false.

Falsy values are: null, undefined, empty string, NaN, 0 and false.

# Check if a String contains a Substring with String.indexOf()

This is a three-step process:

  1. Call the indexOf method on the string, passing it the substring as a parameter.
  2. Conditionally check if the returned value is not equal to -1.
  3. If the returned value is not equal to -1, the string contains the substring.
index.js
const string = 'hello world'; const substring = 'hello'; const index = string.indexOf(substring); console.log(index); // ๐Ÿ‘‰๏ธ 0 if (string.indexOf(substring) !== -1) { // ๐Ÿ‘‰๏ธ substring is contained in the string }

check if string contains substring with indexof

The code for this article is available on GitHub
The String.indexOf method returns the starting index of the substring, or -1 if the substring is not contained in the string.

The substring in the code sample is contained in the string starting at index 0. Therefore the indexOf method returns 0.

Our if block is only run if the String.indexOf method doesn't return -1. The method only returns -1 if the substring is not contained in the string.

# Check if String Doesn't contain a Substring using indexOf()

This is a two-step process:

  1. Use the String.indexOf() method to get the index of the substring in the string.
  2. If the indexOf() method returns -1, the string doesn't contain the substring.
index.js
const str = 'hello world'; if (str.indexOf('bye') === -1) { console.log('โœ… string does not include substring'); } else { console.log('โ›”๏ธ string includes substring'); }

check if string does not contain substring using indexof

The code for this article is available on GitHub

The String.indexOf method returns the index of the first occurrence of a substring in a string.

If the substring is not contained in the string, the method returns -1.

If the String.indexOf() method returns -1, then the substring is not contained in the string, otherwise, the substring is contained in the string.

If you need to check if a string doesn't contain a substring in a case-insensitive manner, convert both strings to lowercase.

index.js
function notContains(string, substring) { return string.toLowerCase().indexOf(substring.toLowerCase()) === -1; } const str = 'hello world'; console.log(notContains(str, 'BYE')); // ๐Ÿ‘‰๏ธ true console.log(notContains(str, 'HEL')); // ๐Ÿ‘‰๏ธ false

We can check if a string doesn't contain a substring in a case-insensitive manner by converting the two strings to the same case.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev