How to check if a string contains a substring using includes() in JavaScript

published: 22 Jul 2022

2 min read

Check if a string contains a substring using includes() in JavaScript

To check if a string contains a substring using String.includes() in JavaScript:

  1. Call the String.includes() method on the string.
  2. Pass the substring as a parameter, e.g. String.includes(substr).
  3. The String.includes() method returns true if the substring is contained in the string. Otherwise, false is returned.
const str = 'React is a popular JavaScript library.'
const substr = 'React'

if (str.includes(substr)) {
  console.log('String contains '${substr}'')
} else {
  console.log('String does not contain '${substr}'')
}
// String contains 'React'

The String.includes() method takes two parameters:

  1. searchString - The substring to search for within this string.
  2. position - The index to start the search from. This parameter is optional. If not specified, the search will start from zero.

Here is a another example that uses String.includes() 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.

The String.includes() method is case-sensitive, which means it acts differently to both uppercase and lowercase characters. The following expression will return false:

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

In the above examples, the second parameter is skipped as we want the search to begin from the start of the string. Most of the time, the second parameter is not required.

However, you can use the second parameter to limit the scope of the search, as shown below:

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 String.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 String.includes() method searches the entire string that is the same as if no index is specified:

const str = 'Welcome to my blog!'

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

The String.includes() method is supported by all modern browsers 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 String.includes() and only loads the polyfill if the browser does not support the String.includes() method.

Read this article to learn about other JavaScript functions that can be used to check the presence of a substring in a string.

How to check if a string contains a substring using includes() in JavaScript | Coding Tips And Tricks

Are we missing something?  Help us improve this article. Reach out to us.

Are you looking for other code tips?

Check out what's on in the category: react, javascript, programming
Check out what's on in the tag: react, javascript, programming