In JavaScript, the includes() method is used to check the presence of a substring. It determines whether a string contains the given substring or not. The includes() method was introduced in E6/ES2015.

This method returns true if the string contains the given characters, otherwise, it returns false.

The includes() method is case-sensitive, which means it acts differently to both uppercase and lowercase characters.

Syntax

str.includes(searchString[, position])

Parameters

The includes() method takes two arguments:

  • searchString — The substring to search for within this string.
  • position — The index to start the search from. This parameter is optional. If not specified the search will start from 0 index.

Return value

A Boolean value true indicating the presence of the given substring or false indicating the absence.

Examples

Here is a simple example that uses the includes() method to check if a word is present in a sentence:

const sentence = 'The JavaScript is a client-side language.';

const word = 'language';

console.log(`The word "${word}" is ${sentence.includes(word) ? 'present' : 'not present'}.`);
// The word is present.

Since the includes() method is case-sensitive, the following expression will return false:

'JavaScript Jobs'.includes('jobs'); // false

In the above examples, we do not pass the second parameter as we want the search to begin from the start of the string. In most of the use-cases, the second parameter is not required.

However, you can use the second parameter to limit the scope of the search. Here is an example that starts the search from mid of the string saving some precious microseconds:

const str = 'How are you doing? I am much better.';

str.includes('much', 20); // true

If the start index is greater than or equal to the length of the string, the includes() method does not perform any search and simply returns false:

'My name is Atta!'.includes('Atta', 20); // false

If the starting index is less than 0, the includes() method searches the entire string which is same as if no index is specified:

const str = 'Welcome to my blog!';

str.includes('blog', -5); // true

Polyfill

The includes() method is supported by all modern browser except Internet Explorer and some legacy Android browsers. However, you can easily add the following polyfill to use it on all browsers:

if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    'use strict';
    if (typeof start !== 'number') {
      start = 0;
    }

    if (start + search.length > this.length) {
      return false;
    } else {
      return this.indexOf(search, start) !== -1;
    }
  };
}

Notice the first line. It feature-detects the support for includes() and only loads the polyfill if the browser does not support the includes() method.

Read Next: 6 ways to check if a string contains a substring in JavaScript to learn about other JavaScript functions that can be used to check the presence of a substring in a string.

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.