DEV Community

Cover image for JavaScript String includes()
Sunil Kumar
Sunil Kumar

Posted on

JavaScript String includes()

String includes() Method

The String.prototype.includes() method in JavaScript is used to check if a particular string is present within another string. It returns true if the specified string is found and false otherwise.

Basic syntax of the string includes() Method

string.includes(searchString [, startIndex]);
Enter fullscreen mode Exit fullscreen mode

Here's an example of how to use the includes() method:

const str = 'Hello, World!';

console.log(str.includes('Hello')); // true
console.log(str.includes('world')); // false (case-sensitive)
Enter fullscreen mode Exit fullscreen mode

The includes() method has the following syntax:

string.includes(searchString[, position])
Enter fullscreen mode Exit fullscreen mode

searchString: The string to search for.
position: (optional) The position in the original string at which to begin the search. Default value is 0.

You can also use the includes() method with the indexOf() method to check if a string is present within another string, but the includes() method is more readable and easier to use in comparison.

It is worth noting that the includes() method is case-sensitive. To check for the presence of a string in a case-insensitive manner, you can convert both the source string and the search string to lowercase using the toLowerCase() method before using the includes() method:

const str = 'Hello, World!';
const searchString = 'hello';

console.log(str.toLowerCase().includes(searchString.toLowerCase())); // true
Enter fullscreen mode Exit fullscreen mode

Another advantage of using the includes() method over the indexOf() method is that it is supported in all modern browsers and can be used in both client-side and server-side JavaScript.

You can also use the includes() method to check if an array contains a certain element. For instance:

const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(3)); // true
Enter fullscreen mode Exit fullscreen mode

In this case, the includes() method will search for the given element in the array and return true if it is found, or false otherwise.

Additionally, the includes() method is also useful when checking for the presence of substrings within a string. For example, you can use it to check if a string ends with a particular suffix:

const str = 'Hello, World!';
const suffix = 'World!';

console.log(str.includes(suffix)); // true
Enter fullscreen mode Exit fullscreen mode

You can also use the includes() method in combination with other string methods, such as slice() or substr(), to extract substrings based on their presence within a string:

const str = 'Hello, World!';
const suffix = 'World!';

if (str.includes(suffix)) {
  const index = str.indexOf(suffix);
  const extracted = str.slice(index);
  console.log(extracted); // World!
}
Enter fullscreen mode Exit fullscreen mode

In conclusion, the includes() method is a useful tool for checking if a string or array contains a specific substring or element.

It is easy to use and provides a more readable alternative to other methods, such as indexOf().

Explanation of its search algorithm

When determining whether a given substring is present in a target string, JavaScript's string includes() method employs a linear search algorithm.

If no startIndex is specified, the method starts at the beginning of the string and searches for the searchString from there.

The method then repeatedly iterates through the string's characters, comparing each one to those in the searchString.

The method returns true, indicating that the searchString is present in the string, if all the characters of the searchString are located in the target string.

The method immediately returns false, indicating that the searchString is not present in the string, if any character of the searchString is not found in the target string.

The includes() method's search algorithm is case-sensitive, which means that when comparing characters from the searchString and the target string, it takes their case into account.

The method will return false if the searchString is case-sensitive and the target string contains a case-insensitive version of the searchString.

Top comments (0)