Web/JavaScript/Reference/Global objects/String/includes

From Get docs


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


The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

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

Specification
ECMAScript (ECMA-262)The definition of 'String.prototype.includes' in that specification.

Browser compatibility

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

Update compatibility data on GitHub

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
includes Chrome

Full support 41

Edge

Full support 12

Firefox Full support 40


Full support 40


No support 18 — 48

Alternate Name'

Alternate Name' Uses the non-standard name: contains

IE

No support No

Opera

Full support 28

Safari

Full support 9

WebView Android

Full support 41

Chrome Android

Full support 41

Firefox Android Full support 40


Full support 40


No support 18 — 48

Alternate Name'

Alternate Name' Uses the non-standard name: contains

Opera Android

Full support 28

Safari iOS

Full support 9

Samsung Internet Android

Full support 4.0

nodejs

Full support 4.0.0

Legend

Full support  
Full support
No support  
No support
Uses a non-standard name.'
Uses a non-standard name.


See also