JavaScript String includes() is a built-in function that checks for the presence of a substring (or sequence of characters) within a string and returns true if a string contains a substring and false otherwise. It is a modern approach for string.indexOf() method.
const str = "Nvidia Company!"; console.log(str.includes("Nvidia")); // Output: true (found as a contiguous sequence) console.log(str.includes("Apple")); // Output: false
In the above code, the substring “Nvidia” exists in the str, so it returns true, but str does not contain “Apple”, which is why it returns false in that case.
Syntax
string.includes(search_string, position)
Parameters
Argument | Description |
search_string (required) |
It is the substring to search for within the calling string. If it is not a string, it is coerced into one. |
position (optional, default 0) | It represents the index at which to start the search.
If you omit it, the search begins from the start of the string (index 0). |
Checking case-sensitiveness
The String includes() method is case-sensitive; even if the strings are the same, but the cases differ, it returns false.
const str = "Nvidia Company!"; console.log(str.includes("nvidia")); // Output: false (case-sensitive)
If you want to ignore case sensitivity, convert the input string to lowercase using string.toLowerCase() method.
const str = "Nvidia is the biggest company!"; lowerStr = str.toLowerCase(); console.log(lowerStr.includes("nvidia")); // Output: true
Now, each character of the input string is converted to lowercase, and the substring “nvidia” matches with the input string’s “nvidia”.
With the “Position” argument
The “position” shifts the search start. The character index of a string starts from 0. By default, it searches from the start, but you can skip it based on your requirements.
Simple example
Let’s say we have an input string called “Amazon” and we want to search for the “zon” substring starting from the beginning of “Amazon” and from the 4th position. Let’s see the outputs.
const main_str = "Amazon"; console.log(main_str.includes("zon")); // Output: true console.log(main_str.includes("zon", 4)); // Output: false
In the first case, we start from the 0th index, so it finds the “zon” substring and returns true.
In the second case, our starting point is the 4th position, and it has an index of 4 (starting from 0).
Therefore, it won’t find the “zon” substring because at the 4th index, the character is “o” and not “z”. So, it returns false.
Complex example
const str = "Nvidia is the biggest company!"; console.log(str.includes("Nvidia", 0)); // Output: true (starts from beginning) console.log(str.includes("Nvidia", 1)); // Output: false (It starts from 0th index but we provided 1st index) console.log(str.includes("company", 23)); // Output: false (It starts from 22nd index but we provided 23rd index) console.log(str.includes("biggest", 13)); // Output: true (starts after 13th index)
In this code, we saw multiple scenarios. In the first scenario, we started searching for the “Nvidia” substring from the 0th index, and since it was found, it returned true.
However, in the second scenario, we started finding “Nvidia” from the 1st index, but it would find “vidia” instead of “Nvidia”, so it returned false as output. The start position matters the most in this case.
The same applies to the third scenario, where we started finding the “company” substring from position 23, which returned false because it started from position 22.
In the final use case, we were searching for the “biggest” substring from the 13th position. Since the string starts at the 14th index, it returns true because it finds the match when it reaches the 14th index.
Empty string behavior
If the substring you are to find is an empty string, the includes() method returns true for any input string because the input string always contains an empty string.
const str = "Nvidia is the biggest company!"; const substr = "" console.log(str.includes(substr)); // Output: true
Position Out of Bounds
If the string has 6 characters and your starting index for searching is 8th, the string includes() method returns false because the position is out of bounds for the input string.
const str = "Krunal"; const substr = "l" console.log(str.includes(substr, 8)); // Output: false
That’s all!