Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla
Your Search Results

    Array.prototype.includes()

    This is an experimental technology, part of the Harmony (ECMAScript 7) proposal.
    Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

    Summary

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

    Syntax

    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; defaults to 0.

    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 (![].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)) {
            return true;
          }
          k++;
        }
        return false;
      };
    }
    

    Specifications

    ES7 proposal: https://github.com/tc39/Array.prototype.includes.

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support Not supported Available in Nightly channel only (since 2014-11-22) Not supported Not supported Not supported
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support Not supported Available in Nightly channel only (since 2014-11-22) Not supported Not supported Not supported Not supported

    See also

    Document Tags and Contributors

    Last updated by: DomenicDenicola,
    Hide Sidebar