JavaScript String includes() method - Check String In A String
In this tutorial, you will get to know how to check if the string contains another string using the includes() string method in javascript.
In many programming problems, you would like to check the occurrence of a string or character in another string or you want to count the number of occurrences of a word in a paragraph.
In such cases use javascript includes() method for the strings to solve the problem.
String includes Method JavaScript
The includes() string method is used to search if a string contains another string within it or not.
The method is applied on a string and the search string is passed as an argument in the method, if the calling string contains the search string then it returns true
else returns false
.
Note: The includes() method is case-sensitive.
Example
var str = "Learning to code";
var search = "code";
// using includes() method
console.log(str.includes(search)); // true
Syntax of includes string method
The syntax of includes() string method is:
str.includes(searchValue, startPosition)
The method accepts 2 arguments:
- searchValue - The string to search for.
- startPosition (optional) - It is the position within the string from where the method starts searching for the searchValue.
Let's search a string from the sentence with the start position value:
Example: Using includes with start position
var str = "Learning to code";
var search = "code";
console.log(str.includes(search, 10));
console.log(str.includes(search, 20));
In the above example, the search for the string ("code") starts from index 10 and 20 in two different lines, in first-line method starts searching from index 10 and finds the search string at index value 12 and in the second line, it starts from index 20 and doesn't find the search string hence return false
.
Case-sensitivity
The includes() method is case sensitive, which means it treats string with the same characters but the different cases as different.
Check out the following example:
Example
var string = "Big Blue Ocean";
// search string "big" with different cases
console.log(string.includes("big")); // false
console.log(string.includes("BIG")); // false
console.log(string.includes("Big")); // true
includes String Method Example
Here are some random string searching:
Example
var quote = "Learning to code is learning to create and innovate";
console.log(quote.includes("Learning to"));
console.log(quote.includes("code is"));
console.log(quote.includes("create and"));
console.log(quote.includes("learning is"));
console.log(quote.includes("ocean"));
How to check if the string includes substring javascript?
Simply use the includes() method to the string and pass the search string as the first argument. If you also want to find an index of the search string within the string then use the indexOf Method method.
Example 1
let case1 = "Plant more trees. Make the planet green.".includes("tree"); // return true
let case2 = "Plant more trees. Make the planet green.".includes("trees"); // return true
let case3 = "Plant more trees. Make the planet green.".includes("Trees"); // return false
console.log(case1, case2, case3);
Example 2
let string = "Hello World!";
if (string.includes("World")) {
console.log("Yes 'World' exists in string.");
}
Alternative to includes Method: indexOf Method
The indexOf method is used to get the index of the search string within a string. It returns the index value at which the string is present.
If the string is not present then it returns -1. You can use this to find if the string is present or not.
Example
const quote = "Learning to code is learning to create and innovate";
const search = "innovate";
if (quote.indexOf(search) > -1) {
console.log("true! Search string is present in given string.");
}
else {
console.log("false! Search string is not present in given string.");
}
Conclusion
To find a string within a given string use the includes method. It returns true
or false
. There is an alternate method to find a string within a string: the indexOf method.