In JavaScript, the includes()
method is a useful string method that allows you to check whether a string contains a specific substring. It returns a boolean value, true
if the substring is found, and false
otherwise. The includes()
method is case-sensitive, meaning it distinguishes between uppercase and lowercase letters.
The syntax for using the includes()
method is as follows:
string.includes(searchValue, startIndex)
The searchValue
parameter is the substring you want to search for within the string, and the startIndex
parameter is an optional parameter that specifies the position within the string to start the search. If not provided, the includes()
method will search the entire string.
Let’s look at some examples to understand how the includes()
method works:
Example 1:
// Using includes() without startIndex
const str = "Hello, World!";
const searchStr = "World";
const result = str.includes(searchStr);
console.log(result); // Output: true
In this example, we have a string "Hello, World!"
and we want to check if it includes the substring "World"
. The includes()
method returns true
because the substring is present in the string.
Example 2:
// Using includes() with startIndex
const str = "Hello, World!";
const searchStr = "Hello";
const startIndex = 7;
const result = str.includes(searchStr, startIndex);
console.log(result); // Output: false
In this example, we have a string "Hello, World!"
and we want to check if it includes the substring "Hello"
starting from the index 7. The includes()
method returns false
because the substring is not found at or after the specified startIndex.
Example 3:
// Case-sensitive search
const str = "Hello, World!";
const searchStr = "hello";
const result = str.includes(searchStr);
console.log(result); // Output: false
In JavaScript, the includes()
method is case-sensitive. In this example, we are searching for the substring "hello"
in the string "Hello, World!"
. Since the case does not match, the includes()
method returns false
.
Example 4:
// Using includes() with multiple search values
const str = "Hello, World!";
const searchStr1 = "Hello";
const searchStr2 = "World";
const result1 = str.includes(searchStr1);
const result2 = str.includes(searchStr2);
console.log(result1); // Output: true
console.log(result2); // Output: true
The includes()
method can also be used to check for multiple search values. In this example, we are checking if the string "Hello, World!"
includes both the substrings "Hello"
and "World"
. Both calls to includes()
return true
because both substrings are present in the string.
It’s important to note that the includes()
method returns true
if the string contains the specified substring at any position. It does not provide information about the exact position of the substring within the string. If you need to find the position of the substring, you can use other methods like indexOf()
or search()
.
Overall, the includes()
method is a convenient way to check if a string contains a specific substring in JavaScript. It provides a simple and efficient solution for substring searching tasks.