Array.prototype.includes

Table of Contents

  1. 1 Array.prototype.includes ( searchElement [ , fromIndex ] )
  2. 2 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )

1Array.prototype.includes ( searchElement [ , fromIndex ] )

Note includes compares searchElement to the elements of the array, in ascending order, using the SameValueZero algorithm, and if found at any position, returns true otherwise, false is returned.

When the includes method is called, the following steps are taken:

  1. Let O be the result of calling ToObject passing the this value as the argument.
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If len is 0, return false.
  6. Let n be ToInteger(fromIndex). (If fromIndex is undefined, this step produces the value 0.)
  7. ReturnIfAbrupt(n).
  8. If n ≥ 0, then
    1. Let k be n.
  9. Else n < 0,
    1. Let k be len + n.
    2. If k < 0, then let k be 0.
  10. Repeat, while k < len
    1. Let elementK be the result of Get(O, ToString(k)).
    2. ReturnIfAbrupt(elementK).
    3. If SameValueZero(searchElement, elementK) is true, return true.
    4. Increase k by 1.
  11. Return false.
Note The optional second argument fromIndex defaults to 0 (i.e. the whole array is searched). If it is greater than or equal to the length of the array, false is returned, i.e. the array will not be searched. If it is negative, it is used as the offset from the end of the array to compute fromIndex. If the computed index is less than 0, the whole array will be searched.

The length property of the includes method is 1.

2%TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )

%TypedArray%.prototype.includes is a distinct function that implements the same algorithm as Array.prototype.includes except that the this object’s [[ArrayLength]] internal slot is accessed in place of performing a [[Get]] of "length". The implementation of the algorithm may be optimized with the knowledge that the this value is an object that has a fixed length and whose integer indexed properties are not sparse. However, such optimization must not introduce any observable changes in the specified behaviour of the algorithm.

This function is not generic. If the this value is not a object with a [[TypedArrayName]] internal slot, a TypeError exception is immediately thrown when this function is called.

The length property of the includes method is 1.