The includes() method determines whether an array includes a certain element, returning true or false as appropriate. 

Syntax

var boolean = array.includes(searchElement[, fromIndex])

Parameters

searchElement
The element to search for.
fromIndex
Optional. The position in this array at which to begin searching for searchElement. A negative value searches from the end of the array. Defaults to 0.

Return value

A Boolean.

Examples

[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true

Polyfill

if (!Array.prototype.includes) {
  Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
    'use strict';
    var O = Object(this);
    var len = parseInt(O.length) || 0;
    if (len === 0) {
      return false;
    }
    var n = parseInt(arguments[1]) || 0;
    var k;
    if (n >= 0) {
      k = n;
    } else {
      k = len + n;
      if (k < 0) {k = 0;}
    }
    var currentElement;
    while (k < len) {
      currentElement = O[k];
      if (searchElement === currentElement ||
         (searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
        return true;
      }
      k++;
    }
    return false;
  };
}

Specifications

Specification Status Comment
ECMAScript 2016 (ECMA-262)
The definition of 'Array.prototype.includes' in that specification.
Standard Initial definition.
ECMAScript 2017 Draft (ECMA-262)
The definition of 'Array.prototype.includes' in that specification.
Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Edge Opera Safari
Basic support

47

43 No support No support 34 9
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support No support

47

43 No support 34 9

47

See also

Document Tags and Contributors

 Last updated by: fscholz,