W3cubDocs

/JavaScript

string.includes

The includes() method determines whether one string may be found within another string, returning true or false as appropriate.

Syntax

str.includes(searchString[, position])

Parameters

searchString
A string to be searched for within str.
position Optional
The position within the string at which to begin searching for searchString. (Defaults to 0.)

Return value

true if the search string is found anywhere within the given string; otherwise, false if not.

Description

This method lets you determine whether or not a string includes another string.

Case-sensitivity

The includes() method is case sensitive. For example, the following expression returns false:

'Blue Whale'.includes('blue')  // returns false

Polyfill

This method has been added to the ECMAScript 2015 specification and may not be available in all JavaScript implementations yet.

However, you can easily polyfill this method:

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

    if (search instanceof RegExp) {
      throw TypeError('first argument must not be a RegExp');
    } 
    if (start === undefined) { start = 0; }
    return this.indexOf(search, start) !== -1;
  };
}

Examples

Using includes()

const str = 'To be, or not to be, that is the question.' 

console.log(str.includes('To be'))        // true
console.log(str.includes('question'))     // true
console.log(str.includes('nonexistent'))  // false
console.log(str.includes('To be', 1))     // false
console.log(str.includes('TO BE'))        // false
console.log(str.includes(''))             // true

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
includes 41 12 40
40
18 — 48
Uses the non-standard name: contains
No 28 9
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
includes 41 41 40
40
18 — 48
Uses the non-standard name: contains
28 9 4.0
Server
Node.js
includes 4.0.0

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes