|
| 1 | +<!DOCTYPE html> |
| 2 | +<meta charset="utf-8"> |
| 3 | +<title>Array.prototype.includes</title> |
| 4 | +<link rel="stylesheet" href="https://bterlson.github.io/ecmarkup/elements.css"> |
| 5 | + |
| 6 | +<emu-clause id="array-prototype-includes"> |
| 7 | + <h1>Array.prototype.includes ( searchElement [ , fromIndex ] )</h1> |
| 8 | + |
| 9 | + <emu-note> |
| 10 | + `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. |
| 11 | + </emu-note> |
| 12 | + |
| 13 | + <p>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.</p> |
| 14 | + |
| 15 | + <p>When the `includes` method is called, the following steps are taken:</p> |
| 16 | + |
| 17 | + <emu-alg> |
| 18 | + 1. Let _O_ be the result of calling ToObject passing the *this* value as the argument. |
| 19 | + 1. ReturnIfAbrupt(_O_). |
| 20 | + 1. Let _len_ be ToLength(Get(_O_, "length")). |
| 21 | + 1. ReturnIfAbrupt(_len_). |
| 22 | + 1. If _len_ is 0, return *false*. |
| 23 | + 1. Let _n_ be ToInteger(_fromIndex_). (If _fromIndex_ is *undefined*, this step produces the value 0.) |
| 24 | + 1. ReturnIfAbrupt(_n_). |
| 25 | + 1. If _n_ ≥ 0, then |
| 26 | + 1. Let _k_ be _n_. |
| 27 | + 1. Else _n_ < 0, |
| 28 | + 1. Let _k_ be _len_ + _n_. |
| 29 | + 1. If _k_ < 0, then let _k_ be 0. |
| 30 | + 1. Repeat, while _k_ _len_ |
| 31 | + 1. Let _elementK_ be the result of Get(_O_, ToString(_k_)). |
| 32 | + 1. ReturnIfAbrupt(_elementK_). |
| 33 | + 1. If SameValueZero(_searchElement_, _elementK_) is *true*, return *true*. |
| 34 | + 1. Increase _k_ by 1. |
| 35 | + 1. Return *false*. |
| 36 | + </emu-alg> |
| 37 | + |
| 38 | + <p>The `length` property of the `includes` method is *1*.</p> |
| 39 | +</emu-clause> |
| 40 | + |
| 41 | +<emu-clause id="typedarray-prototype-includes"> |
| 42 | + <h1>%TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )</h1> |
| 43 | + |
| 44 | + <p>`%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 <code>"length"</code>. 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.</p> |
| 45 | + |
| 46 | + <p>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.</p> |
| 47 | + |
| 48 | + <p>The `length` property of the `includes` method is *1*.</p> |
| 49 | +</emu-clause> |
0 commit comments