Last updated: Mar 1, 2024
Reading timeยท4 min
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.
const string = 'hello world'; const substring = 'hello'; console.log(string.includes(substring)); // ๐๏ธ true if (string.includes(substring)) { // ๐๏ธ substring is contained in the string }
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.
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 }
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.
const str = 'hello world'; if (!str.includes('bye')) { console.log('โ string does not include substring'); } else { console.log('โ๏ธ string includes substring'); }
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
.
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.
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.
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.
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.
true
, in all other cases it returns false
.Falsy values are: null
, undefined
, empty string, NaN
, 0
and false
.
This is a three-step process:
indexOf
method on the string, passing it the substring as a
parameter.-1
.-1
, the string contains the
substring.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 }
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.
indexOf()
This is a two-step process:
String.indexOf()
method to get the index of the substring in the
string.indexOf()
method returns -1
, the string doesn't contain the
substring.const str = 'hello world'; if (str.indexOf('bye') === -1) { console.log('โ string does not include substring'); } else { console.log('โ๏ธ string includes substring'); }
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
.
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.
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.
You can learn more about the related topics by checking out the following tutorials: