22 Indexed Collections

22.1 Array Objects

Array objects are exotic objects that give special treatment to a certain class of property names. See 9.4.2 for a definition of this special treatment.

22.1.1 The Array Constructor

The Array constructor is the %Array% intrinsic object and the initial value of the Array property of the global object. When called as a constructor it creates and initializes a new exotic Array object. When Array is called as a function rather than as a constructor, it also creates and initializes a new Array object. Thus the function call Array() is equivalent to the object creation expression new Array() with the same arguments.

The Array constructor is a single function whose behaviour is overloaded based upon the number and types of its arguments.

The Array constructor is designed to be subclassable. It may be used as the value of an extends clause of a class definition. Subclass constructors that intend to inherit the exotic Array behaviour must include a super call to the Array constructor to initialize subclass instances that are exotic Array objects. However, most of the Array.prototype methods are generic methods that are not dependent upon their this value being an exotic Array object.

The length property of the Array constructor function is 1.

22.1.1.1 Array ( )

This description applies if and only if the Array constructor is called with no arguments.

  1. Let numberOfArgs be the number of arguments passed to this function call.
  2. Assert: numberOfArgs = 0.
  3. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
  4. Let proto be GetPrototypeFromConstructor(newTarget, "%ArrayPrototype%").
  5. ReturnIfAbrupt(proto).
  6. Return ArrayCreate(0, proto).

22.1.1.2 Array (len)

This description applies if and only if the Array constructor is called with exactly one argument.

  1. Let numberOfArgs be the number of arguments passed to this function call.
  2. Assert: numberOfArgs = 1.
  3. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
  4. Let proto be GetPrototypeFromConstructor(newTarget, "%ArrayPrototype%").
  5. ReturnIfAbrupt(proto).
  6. Let array be ArrayCreate(0, proto).
  7. If Type(len) is not Number, then
    1. Let defineStatus be CreateDataProperty(array, "0", len).
    2. Assert: defineStatus is true.
    3. Let intLen be 1.
  8. Else,
    1. Let intLen be ToUint32(len).
    2. If intLenlen, throw a RangeError exception.
  9. Let setStatus be Set(array, "length", intLen, true).
  10. Assert: setStatus is not an abrupt completion.
  11. Return array.

22.1.1.3 Array (...items )

This description applies if and only if the Array constructor is called with at least two arguments.

When the Array function is called the following steps are taken:

  1. Let numberOfArgs be the number of arguments passed to this function call.
  2. Assert: numberOfArgs ≥ 2.
  3. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
  4. Let proto be GetPrototypeFromConstructor(newTarget, "%ArrayPrototype%").
  5. ReturnIfAbrupt(proto).
  6. Let array be ArrayCreate(numberOfArgs, proto).
  7. ReturnIfAbrupt(array).
  8. Let k be 0.
  9. Let items be a zero-origined List containing the argument items in order.
  10. Repeat, while k < numberOfArgs
    1. Let Pk be ToString(k).
    2. Let itemK be items[k].
    3. Let defineStatus be CreateDataProperty(array, Pk, itemK).
    4. Assert: defineStatus is true.
    5. Increase k by 1.
  11. Assert: the value of array's length property is numberOfArgs.
  12. Return array.

22.1.2 Properties of the Array Constructor

The value of the [[Prototype]] internal slot of the Array constructor is the intrinsic object %FunctionPrototype% (19.2.3).

Besides the length property (whose value is 1), the Array constructor has the following properties:

22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )

When the from method is called with argument items and optional arguments mapfn and thisArg the following steps are taken:

  1. Let C be the this value.
  2. If mapfn is undefined, let mapping be false.
  3. else
    1. If IsCallable(mapfn) is false, throw a TypeError exception.
    2. If thisArg was supplied, let T be thisArg; else let T be undefined.
    3. Let mapping be true
  4. Let usingIterator be GetMethod(items, @@iterator).
  5. ReturnIfAbrupt(usingIterator).
  6. If usingIterator is not undefined, then
    1. If IsConstructor(C) is true, then
      1. Let A be Construct(C).
    2. Else,
      1. Let A be ArrayCreate(0).
    3. ReturnIfAbrupt(A).
    4. Let iterator be GetIterator(items, usingIterator).
    5. ReturnIfAbrupt(iterator).
    6. Let k be 0.
    7. Repeat
      1. Let Pk be ToString(k).
      2. Let next be IteratorStep(iterator).
      3. ReturnIfAbrupt(next).
      4. If next is false, then
        1. Let setStatus be Set(A, "length", k, true).
        2. ReturnIfAbrupt(setStatus).
        3. Return A.
      5. Let nextValue be IteratorValue(next).
      6. ReturnIfAbrupt(nextValue).
      7. If mapping is true, then
        1. Let mappedValue be Call(mapfn, T, «nextValue, k»).
        2. If mappedValue is an abrupt completion, return IteratorClose(iterator, mappedValue).
        3. Let mappedValue be mappedValue.[[value]].
      8. Else, let mappedValue be nextValue.
      9. Let defineStatus be CreateDataPropertyOrThrow(A, Pk, mappedValue).
      10. If defineStatus is an abrupt completion, return IteratorClose(iterator, defineStatus).
      11. Increase k by 1.
  7. Assert: items is not an Iterable so assume it is an array-like object.
  8. Let arrayLike be ToObject(items).
  9. ReturnIfAbrupt(arrayLike).
  10. Let len be ToLength(Get(arrayLike, "length")).
  11. ReturnIfAbrupt(len).
  12. If IsConstructor(C) is true, then
    1. Let A be Construct(C, «len»).
  13. Else,
    1. Let A be ArrayCreate(len).
  14. ReturnIfAbrupt(A).
  15. Let k be 0.
  16. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kValue be Get(arrayLike, Pk).
    3. ReturnIfAbrupt(kValue).
    4. If mapping is true, then
      1. Let mappedValue be Call(mapfn, T, «kValue, k»).
      2. ReturnIfAbrupt(mappedValue).
    5. Else, let mappedValue be kValue.
    6. Let defineStatus be CreateDataPropertyOrThrow(A, Pk, mappedValue).
    7. ReturnIfAbrupt(defineStatus).
    8. Increase k by 1.
  17. Let setStatus be Set(A, "length", len, true).
  18. ReturnIfAbrupt(setStatus).
  19. Return A.

The length property of the from method is 1.

NOTE The from function is an intentionally generic factory method; it does not require that its this value be the Array constructor. Therefore it can be transferred to or inherited by any other constructors that may be called with a single numeric argument.

22.1.2.2 Array.isArray ( arg )

The isArray function takes one argument arg, and performs the following steps:

  1. Return IsArray(arg).

22.1.2.3 Array.of ( ...items )

When the of method is called with any number of arguments, the following steps are taken:

  1. Let len be the actual number of arguments passed to this function.
  2. Let items be the List of arguments passed to this function.
  3. Let C be the this value.
  4. If IsConstructor(C) is true, then
    1. Let A be Construct(C, «len»).
  5. Else,
    1. Let A be ArrayCreate(len).
  6. ReturnIfAbrupt(A).
  7. Let k be 0.
  8. Repeat, while k < len
    1. Let kValue be items[k].
    2. Let Pk be ToString(k).
    3. Let defineStatus be CreateDataPropertyOrThrow(A,Pk, kValue).
    4. ReturnIfAbrupt(defineStatus).
    5. Increase k by 1.
  9. Let setStatus be Set(A, "length", len, true).
  10. ReturnIfAbrupt(setStatus).
  11. Return A.

The length property of the of method is 0.

NOTE 1 The items argument is assumed to be a well-formed rest argument value.

NOTE 2 The of function is an intentionally generic factory method; it does not require that its this value be the Array constructor. Therefore it can be transferred to or inherited by other constructors that may be called with a single numeric argument.

22.1.2.4 Array.prototype

The value of Array.prototype is %ArrayPrototype%, the intrinsic Array prototype object (22.1.3).

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

22.1.2.5 get Array [ @@species ]

Array[@@species] is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Return the this value.

The value of the name property of this function is "get [Symbol.species]".

NOTE Array prototype methods normally use their this object's constructor to create a derived object. However, a subclass constructor may over-ride that default behaviour by redefining its @@species property.

22.1.3 Properties of the Array Prototype Object

The Array prototype object is the intrinsic object %ArrayPrototype%. The Array prototype object is an Array exotic objects and has the internal methods specified for such objects. It has a length property whose initial value is 0 and whose attributes are { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }.

The value of the [[Prototype]] internal slot of the Array prototype object is the intrinsic object %ObjectPrototype%.

NOTE The Array prototype object is specified to be an Array exotic object to ensure compatibility with ECMAScript code that was created prior to the ECMAScript 2015 specification.

22.1.3.1 Array.prototype.concat ( ...arguments )

When the concat method is called with zero or more arguments, it returns an array containing the array elements of the object followed by the array elements of each argument in order.

The following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let A be ArraySpeciesCreate(O, 0).
  4. ReturnIfAbrupt(A).
  5. Let n be 0.
  6. Let items be a List whose first element is O and whose subsequent elements are, in left to right order, the arguments that were passed to this function invocation.
  7. Repeat, while items is not empty
    1. Remove the first element from items and let E be the value of the element.
    2. Let spreadable be IsConcatSpreadable(E).
    3. ReturnIfAbrupt(spreadable).
    4. If spreadable is true, then
      1. Let k be 0.
      2. Let len be ToLength(Get(E, "length")).
      3. ReturnIfAbrupt(len).
      4. If n + len > 253-1, throw a TypeError exception.
      5. Repeat, while k < len
        1. Let P be ToString(k).
        2. Let exists be HasProperty(E, P).
        3. ReturnIfAbrupt(exists).
        4. If exists is true, then
          1. Let subElement be Get(E, P).
          2. ReturnIfAbrupt(subElement).
          3. Let status be CreateDataPropertyOrThrow (A, ToString(n), subElement).
          4. ReturnIfAbrupt(status).
        5. Increase n by 1.
        6. Increase k by 1.
    5. Else E is added as a single item rather than spread,
      1. If n≥253-1, throw a TypeError exception.
      2. Let status be CreateDataPropertyOrThrow (A, ToString(n), E).
      3. ReturnIfAbrupt(status).
      4. Increase n by 1.
  8. Let setStatus be Set(A, "length", n, true).
  9. ReturnIfAbrupt(setStatus).
  10. Return A.

The length property of the concat method is 1.

NOTE 1 The explicit setting of the length property in step 8 is necessary to ensure that its value is correct in situations where the trailing elements of the result Array are not present.

NOTE 2 The concat function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.1.1 Runtime Semantics: IsConcatSpreadable ( O )

The abstract operation IsConcatSpreadable with argument O performs the following steps:

  1. If Type(O) is not Object, return false.
  2. Let spreadable be Get(O, @@isConcatSpreadable).
  3. ReturnIfAbrupt(spreadable).
  4. If spreadable is not undefined, return ToBoolean(spreadable).
  5. Return IsArray(O).

22.1.3.2 Array.prototype.constructor

The initial value of Array.prototype.constructor is the intrinsic object %Array%.

22.1.3.3 Array.prototype.copyWithin (target, start [ , end ] )

The copyWithin method takes up to three arguments target, start and end.

NOTE 1 The end argument is optional with the length of the this object as its default value. If target is negative, it is treated as length+target where length is the length of the array. If start is negative, it is treated as length+start. If end is negative, it is treated as length+end.

The following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. Let relativeTarget be ToInteger(target).
  6. ReturnIfAbrupt(relativeTarget).
  7. If relativeTarget < 0, let to be max((len + relativeTarget),0); else let to be min(relativeTarget, len).
  8. Let relativeStart be ToInteger(start).
  9. ReturnIfAbrupt(relativeStart).
  10. If relativeStart < 0, let from be max((len + relativeStart),0); else let from be min(relativeStart, len).
  11. If end is undefined, let relativeEnd be len; else let relativeEnd be ToInteger(end).
  12. ReturnIfAbrupt(relativeEnd).
  13. If relativeEnd < 0, let final be max((len + relativeEnd),0); else let final be min(relativeEnd, len).
  14. Let count be min(final-from, len-to).
  15. If from<to and to<from+count
    1. Let direction be -1.
    2. Let from be from + count -1.
    3. Let to be to + count -1.
  16. Else,
    1. Let direction = 1.
  17. Repeat, while count > 0
    1. Let fromKey be ToString(from).
    2. Let toKey be ToString(to).
    3. Let fromPresent be HasProperty(O, fromKey).
    4. ReturnIfAbrupt(fromPresent).
    5. If fromPresent is true, then
      1. Let fromVal be Get(O, fromKey).
      2. ReturnIfAbrupt(fromVal).
      3. Let setStatus be Set(O, toKey, fromVal, true).
      4. ReturnIfAbrupt(setStatus).
    6. Else fromPresent is false,
      1. Let deleteStatus be DeletePropertyOrThrow(O, toKey).
      2. ReturnIfAbrupt(deleteStatus).
    7. Let from be from + direction.
    8. Let to be to + direction.
    9. Let count be count − 1.
  18. Return O.

The length property of the copyWithin method is 2.

NOTE 2 The copyWithin function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.4 Array.prototype.entries ( )

The following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Return CreateArrayIterator(O, "key+value").

22.1.3.5 Array.prototype.every ( callbackfn [ , thisArg] )

NOTE 1 callbackfn should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. every calls callbackfn once for each element present in the array, in ascending order, until it finds one where callbackfn returns false. If such an element is found, every immediately returns false. Otherwise, if callbackfn returned true for all elements, every will return true. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.

callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.

every does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

The range of elements processed by every is set before the first call to callbackfn. Elements which are appended to the array after the call to every begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time every visits them; elements that are deleted after the call to every begins and before being visited are not visited. every acts like the "for all" quantifier in mathematics. In particular, for an empty array, it returns true.

When the every method is called with one or two arguments, the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If IsCallable(callbackfn) is false, throw a TypeError exception.
  6. If thisArg was supplied, let T be thisArg; else let T be undefined.
  7. Let k be 0.
  8. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kPresent be HasProperty(O, Pk).
    3. ReturnIfAbrupt(kPresent).
    4. If kPresent is true, then
      1. Let kValue be Get(O, Pk).
      2. ReturnIfAbrupt(kValue).
      3. Let testResult be ToBoolean(Call(callbackfn, T, «kValue, k, O»)).
      4. ReturnIfAbrupt(testResult).
      5. If testResult is false, return false.
    5. Increase k by 1.
  9. Return true.

The length property of the every method is 1.

NOTE 2 The every function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.6 Array.prototype.fill (value [ , start [ , end ] ] )

The fill method takes up to three arguments value, start and end.

NOTE 1 The start and end arguments are optional with default values of 0 and the length of the this object. If start is negative, it is treated as length+start where length is the length of the array. If end is negative, it is treated as length+end.

The following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. Let relativeStart be ToInteger(start).
  6. ReturnIfAbrupt(relativeStart).
  7. If relativeStart < 0, let k be max((len + relativeStart),0); else let k be min(relativeStart, len).
  8. If end is undefined, let relativeEnd be len; else let relativeEnd be ToInteger(end).
  9. ReturnIfAbrupt(relativeEnd).
  10. If relativeEnd < 0, let final be max((len + relativeEnd),0); else let final be min(relativeEnd, len).
  11. Repeat, while k < final
    1. Let Pk be ToString(k).
    2. Let setStatus be Set(O, Pk, value, true).
    3. ReturnIfAbrupt(setStatus).
    4. Increase k by 1.
  12. Return O.

The length property of the fill method is 1.

NOTE 2 The fill function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.7 Array.prototype.filter ( callbackfn [ , thisArg ] )

NOTE 1 callbackfn should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. filter calls callbackfn once for each element in the array, in ascending order, and constructs a new array of all the values for which callbackfn returns true. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.

callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.

filter does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

The range of elements processed by filter is set before the first call to callbackfn. Elements which are appended to the array after the call to filter begins will not be visited by callbackfn. If existing elements of the array are changed their value as passed to callbackfn will be the value at the time filter visits them; elements that are deleted after the call to filter begins and before being visited are not visited.

When the filter method is called with one or two arguments, the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If IsCallable(callbackfn) is false, throw a TypeError exception.
  6. If thisArg was supplied, let T be thisArg; else let T be undefined.
  7. Let A be ArraySpeciesCreate(O, 0).
  8. ReturnIfAbrupt(A).
  9. Let k be 0.
  10. Let to be 0.
  11. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kPresent be HasProperty(O, Pk).
    3. ReturnIfAbrupt(kPresent).
    4. If kPresent is true, then
      1. Let kValue be Get(O, Pk).
      2. ReturnIfAbrupt(kValue).
      3. Let selected be ToBoolean(Call(callbackfn, T, «kValue, k, O»)).
      4. ReturnIfAbrupt(selected).
      5. If selected is true, then
        1. Let status be CreateDataPropertyOrThrow (A, ToString(to), kValue).
        2. ReturnIfAbrupt(status).
        3. Increase to by 1.
    5. Increase k by 1.
  12. Return A.

The length property of the filter method is 1.

NOTE 2 The filter function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.8 Array.prototype.find ( predicate [ , thisArg ] )

The find method is called with one or two arguments, predicate and thisArg.

NOTE 1 predicate should be a function that accepts three arguments and returns a value that is coercible to a Boolean value. find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.

If a thisArg parameter is provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

predicate is called with three arguments: the value of the element, the index of the element, and the object being traversed.

find does not directly mutate the object on which it is called but the object may be mutated by the calls to predicate.

The range of elements processed by find is set before the first call to callbackfn. Elements that are appended to the array after the call to find begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to predicate will be the value at the time that find visits them.

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

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If IsCallable(predicate) is false, throw a TypeError exception.
  6. If thisArg was supplied, let T be thisArg; else let T be undefined.
  7. Let k be 0.
  8. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kValue be Get(O, Pk).
    3. ReturnIfAbrupt(kValue).
    4. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
    5. ReturnIfAbrupt(testResult).
    6. If testResult is true, return kValue.
    7. Increase k by 1.
  9. Return undefined.

The length property of the find method is 1.

NOTE 2 The find function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.9 Array.prototype.findIndex ( predicate [ , thisArg ] )

NOTE 1 predicate should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. findIndex calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns the index of that element value. Otherwise, findIndex returns -1.

If a thisArg parameter is provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

predicate is called with three arguments: the value of the element, the index of the element, and the object being traversed.

findIndex does not directly mutate the object on which it is called but the object may be mutated by the calls to predicate.

The range of elements processed by findIndex is set before the first call to callbackfn. Elements that are appended to the array after the call to findIndex begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to predicate will be the value at the time that findIndex visits them.

When the findIndex method is called with one or two arguments, the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If IsCallable(predicate) is false, throw a TypeError exception.
  6. If thisArg was supplied, let T be thisArg; else let T be undefined.
  7. Let k be 0.
  8. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kValue be Get(O, Pk).
    3. ReturnIfAbrupt(kValue).
    4. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
    5. ReturnIfAbrupt(testResult).
    6. If testResult is true, return k.
    7. Increase k by 1.
  9. Return -1.

The length property of the findIndex method is 1.

NOTE 2 The findIndex function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )

NOTE 1 callbackfn should be a function that accepts three arguments. forEach calls callbackfn once for each element present in the array, in ascending order. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.

callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.

forEach does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

When the forEach method is called with one or two arguments, the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If IsCallable(callbackfn) is false, throw a TypeError exception.
  6. If thisArg was supplied, let T be thisArg; else let T be undefined.
  7. Let k be 0.
  8. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kPresent be HasProperty(O, Pk).
    3. ReturnIfAbrupt(kPresent).
    4. If kPresent is true, then
      1. Let kValue be Get(O, Pk).
      2. ReturnIfAbrupt(kValue).
      3. Let funcResult be Call(callbackfn, T, «kValue, k, O»).
      4. ReturnIfAbrupt(funcResult).
    5. Increase k by 1.
  9. Return undefined.

The length property of the forEach method is 1.

NOTE 2 The forEach function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.11 Array.prototype.indexOf ( searchElement [ , fromIndex ] )

NOTE 1 indexOf compares searchElement to the elements of the array, in ascending order, using the Strict Equality Comparison algorithm (7.2.13), and if found at one or more indices, returns the smallest such index; otherwise, −1 is returned.

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, −1 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.

When the indexOf method is called with one or two arguments, the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If len is 0, return −1.
  6. If argument fromIndex was passed let n be ToInteger(fromIndex); else let n be 0.
  7. ReturnIfAbrupt(n).
  8. If nlen, return −1.
  9. If n ≥ 0, then
    1. Let k be n.
  10. Else n<0,
    1. Let k be len - abs(n).
    2. If k < 0, let k be 0.
  11. Repeat, while k<len
    1. Let kPresent be HasProperty(O, ToString(k)).
    2. ReturnIfAbrupt(kPresent).
    3. If kPresent is true, then
      1. Let elementK be Get(O, ToString(k)).
      2. ReturnIfAbrupt(elementK).
      3. Let same be the result of performing Strict Equality Comparison searchElement === elementK.
      4. If same is true, return k.
    4. Increase k by 1.
  12. Return -1.

The length property of the indexOf method is 1.

NOTE 2 The indexOf function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.12 Array.prototype.join (separator)

NOTE 1 The elements of the array are converted to Strings, and these Strings are then concatenated, separated by occurrences of the separator. If no separator is provided, a single comma is used as the separator.

The join method takes one argument, separator, and performs the following steps:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If separator is undefined, let separator be the single-element String ",".
  6. Let sep be ToString(separator).
  7. ReturnIfAbrupt(sep).
  8. If len is zero, return the empty String.
  9. Let element0 be Get(O, "0").
  10. If element0 is undefined or null, let R be the empty String; otherwise, let R be ToString(element0).
  11. ReturnIfAbrupt(R).
  12. Let k be 1.
  13. Repeat, while k < len
    1. Let S be the String value produced by concatenating R and sep.
    2. Let element be Get(O, ToString(k)).
    3. If element is undefined or null, let next be the empty String; otherwise, let next be ToString(element).
    4. ReturnIfAbrupt(next).
    5. Let R be a String value produced by concatenating S and next.
    6. Increase k by 1.
  14. Return R.

The length property of the join method is 1.

NOTE 2 The join function is intentionally generic; it does not require that its this value be an Array object. Therefore, it can be transferred to other kinds of objects for use as a method.

22.1.3.13 Array.prototype.keys ( )

The following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Return CreateArrayIterator(O, "key").

22.1.3.14 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )

NOTE 1 lastIndexOf compares searchElement to the elements of the array in descending order using the Strict Equality Comparison algorithm (7.2.13), and if found at one or more indices, returns the largest such index; otherwise, −1 is returned.

The optional second argument fromIndex defaults to the array's length minus one (i.e. the whole array is searched). If it is greater than or equal to the length of the array, the whole array will 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, −1 is returned.

When the lastIndexOf method is called with one or two arguments, the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If len is 0, return -1.
  6. If argument fromIndex was passed let n be ToInteger(fromIndex); else let n be len-1.
  7. ReturnIfAbrupt(n).
  8. If n ≥ 0, let k be min(n, len – 1).
  9. Else n < 0,
    1. Let k be len - abs(n).
  10. Repeat, while k≥ 0
    1. Let kPresent be HasProperty(O, ToString(k)).
    2. ReturnIfAbrupt(kPresent).
    3. If kPresent is true, then
      1. Let elementK be Get(O, ToString(k)).
      2. ReturnIfAbrupt(elementK).
      3. Let same be the result of performing Strict Equality Comparison
        searchElement === elementK.
      4. If same is true, return k.
    4. Decrease k by 1.
  11. Return -1.

The length property of the lastIndexOf method is 1.

NOTE 2 The lastIndexOf function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.15 Array.prototype.map ( callbackfn [ , thisArg ] )

NOTE 1 callbackfn should be a function that accepts three arguments. map calls callbackfn once for each element in the array, in ascending order, and constructs a new Array from the results. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.

callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.

map does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

The range of elements processed by map is set before the first call to callbackfn. Elements which are appended to the array after the call to map begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time map visits them; elements that are deleted after the call to map begins and before being visited are not visited.

When the map method is called with one or two arguments, the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If IsCallable(callbackfn) is false, throw a TypeError exception.
  6. If thisArg was supplied, let T be thisArg; else let T be undefined.
  7. Let A be ArraySpeciesCreate(O, len).
  8. ReturnIfAbrupt(A).
  9. Let k be 0.
  10. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kPresent be HasProperty(O, Pk).
    3. ReturnIfAbrupt(kPresent).
    4. If kPresent is true, then
      1. Let kValue be Get(O, Pk).
      2. ReturnIfAbrupt(kValue).
      3. Let mappedValue be Call(callbackfn, T, «kValue, k, O»).
      4. ReturnIfAbrupt(mappedValue).
      5. Let status be CreateDataPropertyOrThrow (A, Pk, mappedValue).
      6. ReturnIfAbrupt(status).
    5. Increase k by 1.
  11. Return A.

The length property of the map method is 1.

NOTE 2 The map function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.16 Array.prototype.pop ( )

NOTE 1 The last element of the array is removed from the array and returned.

When the pop method is called the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If len is zero,
    1. Let setStatus be Set(O, "length", 0, true).
    2. ReturnIfAbrupt(setStatus).
    3. Return undefined.
  6. Else len > 0,
    1. Let newLen be len–1.
    2. Let indx be ToString(newLen).
    3. Let element be Get(O, indx).
    4. ReturnIfAbrupt(element).
    5. Let deleteStatus be DeletePropertyOrThrow(O, indx).
    6. ReturnIfAbrupt(deleteStatus).
    7. Let setStatus be Set(O, "length", newLen, true).
    8. ReturnIfAbrupt(setStatus).
    9. Return element.

NOTE 2 The pop function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.17 Array.prototype.push ( ...items )

NOTE 1 The arguments are appended to the end of the array, in the order in which they appear. The new length of the array is returned as the result of the call.

When the push method is called with zero or more arguments the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. Let items be a List whose elements are, in left to right order, the arguments that were passed to this function invocation.
  6. Let argCount be the number of elements in items.
  7. If len + argCount > 253-1, throw a TypeError exception.
  8. Repeat, while items is not empty
    1. Remove the first element from items and let E be the value of the element.
    2. Let setStatus be Set(O, ToString(len), E, true).
    3. ReturnIfAbrupt(setStatus).
    4. Let len be len+1.
  9. Let setStatus be Set(O, "length", len, true).
  10. ReturnIfAbrupt(setStatus).
  11. Return len.

The length property of the push method is 1.

NOTE 2 The push function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.18 Array.prototype.reduce ( callbackfn [ , initialValue ] )

NOTE 1 callbackfn should be a function that takes four arguments. reduce calls the callback, as a function, once for each element present in the array, in ascending order.

callbackfn is called with four arguments: the previousValue (value from the previous call to callbackfn), the currentValue (value of the current element), the currentIndex, and the object being traversed. The first time that callback is called, the previousValue and currentValue can be one of two values. If an initialValue was provided in the call to reduce, then previousValue will be equal to initialValue and currentValue will be equal to the first value in the array. If no initialValue was provided, then previousValue will be equal to the first value in the array and currentValue will be equal to the second. It is a TypeError if the array contains no elements and initialValue is not provided.

reduce does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

The range of elements processed by reduce is set before the first call to callbackfn. Elements that are appended to the array after the call to reduce begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time reduce visits them; elements that are deleted after the call to reduce begins and before being visited are not visited.

When the reduce method is called with one or two arguments, the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If IsCallable(callbackfn) is false, throw a TypeError exception.
  6. If len is 0 and initialValue is not present, throw a TypeError exception.
  7. Let k be 0.
  8. If initialValue is present, then
    1. Set accumulator to initialValue.
  9. Else initialValue is not present,
    1. Let kPresent be false.
    2. Repeat, while kPresent is false and k < len
      1. Let Pk be ToString(k).
      2. Let kPresent be HasProperty(O, Pk).
      3. ReturnIfAbrupt(kPresent).
      4. If kPresent is true, then
        1. Let accumulator be Get(O, Pk).
        2. ReturnIfAbrupt(accumulator).
      5. Increase k by 1.
    3. If kPresent is false, throw a TypeError exception.
  10. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kPresent be HasProperty(O, Pk).
    3. ReturnIfAbrupt(kPresent).
    4. If kPresent is true, then
      1. Let kValue be Get(O, Pk).
      2. ReturnIfAbrupt(kValue).
      3. Let accumulator be Call(callbackfn, undefined, «accumulator, kValue, k, O»).
      4. ReturnIfAbrupt(accumulator).
    5. Increase k by 1.
  11. Return accumulator.

The length property of the reduce method is 1.

NOTE 2 The reduce function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.19 Array.prototype.reduceRight ( callbackfn [ , initialValue ] )

NOTE 1 callbackfn should be a function that takes four arguments. reduceRight calls the callback, as a function, once for each element present in the array, in descending order.

callbackfn is called with four arguments: the previousValue (value from the previous call to callbackfn), the currentValue (value of the current element), the currentIndex, and the object being traversed. The first time the function is called, the previousValue and currentValue can be one of two values. If an initialValue was provided in the call to reduceRight, then previousValue will be equal to initialValue and currentValue will be equal to the last value in the array. If no initialValue was provided, then previousValue will be equal to the last value in the array and currentValue will be equal to the second-to-last value. It is a TypeError if the array contains no elements and initialValue is not provided.

reduceRight does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

The range of elements processed by reduceRight is set before the first call to callbackfn. Elements that are appended to the array after the call to reduceRight begins will not be visited by callbackfn. If existing elements of the array are changed by callbackfn, their value as passed to callbackfn will be the value at the time reduceRight visits them; elements that are deleted after the call to reduceRight begins and before being visited are not visited.

When the reduceRight method is called with one or two arguments, the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If IsCallable(callbackfn) is false, throw a TypeError exception.
  6. If len is 0 and initialValue is not present, throw a TypeError exception.
  7. Let k be len-1.
  8. If initialValue is present, then
    1. Set accumulator to initialValue.
  9. Else initialValue is not present,
    1. Let kPresent be false.
    2. Repeat, while kPresent is false and k ≥ 0
      1. Let Pk be ToString(k).
      2. Let kPresent be HasProperty(O, Pk).
      3. ReturnIfAbrupt(kPresent).
      4. If kPresent is true, then
        1. Let accumulator be Get(O, Pk).
        2. ReturnIfAbrupt(accumulator).
      5. Decrease k by 1.
    3. If kPresent is false, throw a TypeError exception.
  10. Repeat, while k ≥ 0
    1. Let Pk be ToString(k).
    2. Let kPresent be HasProperty(O, Pk).
    3. ReturnIfAbrupt(kPresent).
    4. If kPresent is true, then
      1. Let kValue be Get(O, Pk).
      2. ReturnIfAbrupt(kValue).
      3. Let accumulator be Call(callbackfn, undefined, «accumulator, kValue, k, »).
      4. ReturnIfAbrupt(accumulator).
    5. Decrease k by 1.
  11. Return accumulator.

The length property of the reduceRight method is 1.

NOTE 2 The reduceRight function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.20 Array.prototype.reverse ( )

NOTE 1 The elements of the array are rearranged so as to reverse their order. The object is returned as the result of the call.

When the reverse method is called the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. Let middle be floor(len/2).
  6. Let lower be 0.
  7. Repeat, while lowermiddle
    1. Let upper be lenlower −1.
    2. Let upperP be ToString(upper).
    3. Let lowerP be ToString(lower).
    4. Let lowerExists be HasProperty(O, lowerP).
    5. ReturnIfAbrupt(lowerExists).
    6. If lowerExists is true, then
      1. Let lowerValue be Get(O, lowerP).
      2. ReturnIfAbrupt(lowerValue).
    7. Let upperExists be HasProperty(O, upperP).
    8. ReturnIfAbrupt(upperExists).
    9. If upperExists is true, then
      1. Let upperValue be Get(O, upperP).
      2. ReturnIfAbrupt(upperValue).
    10. If lowerExists is true and upperExists is true, then
      1. Let setStatus be Set(O, lowerP, upperValue, true).
      2. ReturnIfAbrupt(setStatus).
      3. Let setStatus be Set(O, upperP, lowerValue, true).
      4. ReturnIfAbrupt(setStatus).
    11. Else if lowerExists is false and upperExists is true, then
      1. Let setStatus be Set(O, lowerP, upperValue, true).
      2. ReturnIfAbrupt(setStatus).
      3. Let deleteStatus be DeletePropertyOrThrow (O, upperP).
      4. ReturnIfAbrupt(deleteStatus).
    12. Else if lowerExists is true and upperExists is false, then
      1. Let deleteStatus be DeletePropertyOrThrow (O, lowerP).
      2. ReturnIfAbrupt(deleteStatus).
      3. Let setStatus be Set(O, upperP, lowerValue, true).
      4. ReturnIfAbrupt(setStatus).
    13. Else both lowerExists and upperExists are false,
      1. No action is required.
    14. Increase lower by 1.
  8. Return O .

NOTE 2 The reverse function is intentionally generic; it does not require that its this value be an Array object. Therefore, it can be transferred to other kinds of objects for use as a method.

22.1.3.21 Array.prototype.shift ( )

NOTE 1 The first element of the array is removed from the array and returned.

When the shift method is called the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If len is zero, then
    1. Let setStatus be Set(O, "length", 0, true).
    2. ReturnIfAbrupt(setStatus).
    3. Return undefined.
  6. Let first be Get(O, "0").
  7. ReturnIfAbrupt(first).
  8. Let k be 1.
  9. Repeat, while k < len
    1. Let from be ToString(k).
    2. Let to be ToString(k–1).
    3. Let fromPresent be HasProperty(O, from).
    4. ReturnIfAbrupt(fromPresent).
    5. If fromPresent is true, then
      1. Let fromVal be Get(O, from).
      2. ReturnIfAbrupt(fromVal).
      3. Let setStatus be Set(O, to, fromVal, true).
      4. ReturnIfAbrupt(setStatus).
    6. Else fromPresent is false,
      1. Let deleteStatus be DeletePropertyOrThrow(O, to).
      2. ReturnIfAbrupt(deleteStatus).
    7. Increase k by 1.
  10. Let deleteStatus be DeletePropertyOrThrow(O, ToString(len–1)).
  11. ReturnIfAbrupt(deleteStatus).
  12. Let setStatus be Set(O, "length", len–1, true).
  13. ReturnIfAbrupt(setStatus).
  14. Return first.

NOTE 2 The shift function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.22 Array.prototype.slice (start, end)

NOTE 1 The slice method takes two arguments, start and end, and returns an array containing the elements of the array from element start up to, but not including, element end (or through the end of the array if end is undefined). If start is negative, it is treated as length+start where length is the length of the array. If end is negative, it is treated as length+end where length is the length of the array.

The following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. Let relativeStart be ToInteger(start).
  6. ReturnIfAbrupt(relativeStart).
  7. If relativeStart < 0, let k be max((len + relativeStart),0); else let k be min(relativeStart, len).
  8. If end is undefined, let relativeEnd be len; else let relativeEnd be ToInteger(end).
  9. ReturnIfAbrupt(relativeEnd).
  10. If relativeEnd < 0, let final be max((len + relativeEnd),0); else let final be min(relativeEnd, len).
  11. Let count be max(finalk, 0).
  12. Let A be ArraySpeciesCreate(O, count).
  13. ReturnIfAbrupt(A).
  14. Let n be 0.
  15. Repeat, while k < final
    1. Let Pk be ToString(k).
    2. Let kPresent be HasProperty(O, Pk).
    3. ReturnIfAbrupt(kPresent).
    4. If kPresent is true, then
      1. Let kValue be Get(O, Pk).
      2. ReturnIfAbrupt(kValue).
      3. Let status be CreateDataPropertyOrThrow(A, ToString(n), kValue ).
      4. ReturnIfAbrupt(status).
    5. Increase k by 1.
    6. Increase n by 1.
  16. Let setStatus be Set(A, "length", n, true).
  17. ReturnIfAbrupt(setStatus).
  18. Return A.

The length property of the slice method is 2.

NOTE 2 The explicit setting of the length property of the result Array in step 16 is necessary to ensure that its value is correct in situations where the trailing elements of the result Array are not present.

NOTE 3 The slice function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.23 Array.prototype.some ( callbackfn [ , thisArg ] )

NOTE 1 callbackfn should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. some calls callbackfn once for each element present in the array, in ascending order, until it finds one where callbackfn returns true. If such an element is found, some immediately returns true. Otherwise, some returns false. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.

callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.

some does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

The range of elements processed by some is set before the first call to callbackfn. Elements that are appended to the array after the call to some begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time that some visits them; elements that are deleted after the call to some begins and before being visited are not visited. some acts like the "exists" quantifier in mathematics. In particular, for an empty array, it returns false.

When the some method is called with one or two arguments, the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. If IsCallable(callbackfn) is false, throw a TypeError exception.
  6. If thisArg was supplied, let T be thisArg; else let T be undefined.
  7. Let k be 0.
  8. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kPresent be HasProperty(O, Pk).
    3. ReturnIfAbrupt(kPresent).
    4. If kPresent is true, then
      1. Let kValue be Get(O, Pk).
      2. ReturnIfAbrupt(kValue).
      3. Let testResult be ToBoolean(Call(callbackfn, T, «kValue, k, and O»)).
      4. ReturnIfAbrupt(testResult).
      5. If testResult is true, return true.
    5. Increase k by 1.
  9. Return false.

The length property of the some method is 1.

NOTE 2 The some function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.24 Array.prototype.sort (comparefn)

The elements of this array are sorted. The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order). If comparefn is not undefined, it should be a function that accepts two arguments x and y and returns a negative value if x < y, zero if x = y, or a positive value if x > y.

Upon entry, the following steps are performed to initialize evaluation of the sort function:

  1. Let obj be ToObject(this value).
  2. Let len be ToLength(Get(obj, "length")).
  3. ReturnIfAbrupt(len).

Within this specification of the sort method, an object, obj, is said to be sparse if the following algorithm returns true:

  1. For each integer i in the range 0≤i< len
    1. Let elem be obj.[[GetOwnProperty]](ToString(i)).
    2. If elem is undefined, return true.
  2. Return false.

The sort order is the ordering, after completion of this function, of the integer indexed property values of obj whose integer indexes are less than len. The result of the sort function is then determined as follows:

If comparefn is not undefined and is not a consistent comparison function for the elements of this array (see below), the sort order is implementation-defined. The sort order is also implementation-defined if comparefn is undefined and SortCompare ({ REF _Ref393437395 \n \h }22.1.3.24.1) does not act as a consistent comparison function.

Let proto be obj.[[GetPrototypeOf]](). If proto is not null and there exists an integer j such that all of the conditions below are satisfied then the sort order is implementation-defined:

The sort order is also implementation defined if obj is sparse and any of the following conditions are true:

  • IsExtensible(obj) is false.

  • Any integer index property of obj whose name is a nonnegative integer less than len is a data property whose [[Configurable]] attribute is false.

The sort order is also implementation defined if any of the following conditions are true:

  • If obj is an exotic object (including Proxy exotic objects) whose behaviour for [[Get]], [[Set]], [[Delete]], and [[GetOwnProperty]] is not the ordinary object implementation of these internal methods.

  • If any index index property of obj whose name is a nonnegative integer less than len is an accessor property or is a data property whose [[Writable]] attribute is false.

  • If comparefn is undefined and the application of ToString to any value passed as an argument to SortCompare modifies obj or any object on obj's prototype chain.

  • If comparefn is undefined and all applications of ToString, to any specific value passed as an argument to SortCompare, do not produce the same result.

The following steps are taken:

  1. Perform an implementation-dependent sequence of calls to the [[Get]] and [[Set]] internal methods of obj, to the DeletePropertyOrThrow and HasOwnProperty abstract operation with obj as the first argument, and to SortCompare (described below), such that:
    • The property key argument for each call to [[Get]], [[Set]], HasOwnProperty, or DeletePropertyOrThrow is the string representation of a nonnegative integer less than len.

    • The arguments for calls to SortCompare are values returned by a previous call to the [[Get]] internal method, unless the properties accessed by those previous calls did not exist according to HasOwnProperty. If both perspective arguments to SortCompare correspond to non-existent properties, use +0 instead of calling SortCompare. If only the first perspective argument is non-existent use +1. If only the second perspective argument is non-existent use −1.

    • If obj is not sparse then DeletePropertyOrThrow must not be called.

    • If any [[Set]] call returns false a TypeError exception is thrown.

    • If an abrupt completion is returned from any of these operations, it is immediately returned as the value of this function.

  2. Return obj.

Unless the sort order is specified above to be implementation-defined, the returned object must have the following two characteristics:

  • There must be some mathematical permutation π of the nonnegative integers less than len, such that for every nonnegative integer j less than len, if property old[j] existed, then new[π(j)] is exactly the same value as old[j]. But if property old[j] did not exist, then new[π(j)] does not exist.

  • Then for all nonnegative integers j and k, each less than len, if SortCompare(old[j], old[k]) < 0 (see SortCompare below), then new[π(j)] < new[π(k)].

Here the notation old[j] is used to refer to the hypothetical result of calling the [[Get]] internal method of obj with argument j before this function is executed, and the notation new[j] to refer to the hypothetical result of calling the [[Get]] internal method of obj with argument j after this function has been executed.

A function comparefn is a consistent comparison function for a set of values S if all of the requirements below are met for all values a, b, and c (possibly the same value) in the set S: The notation a <CF b means comparefn(a,b) < 0; a =CF b means comparefn(a,b) = 0 (of either sign); and a >CF b means comparefn(a,b) > 0.

  • Calling comparefn(a,b) always returns the same value v when given a specific pair of values a and b as its two arguments. Furthermore, Type(v) is Number, and v is not NaN. Note that this implies that exactly one of a <CF b, a =CF b, and a >CF b will be true for a given pair of a and b.

  • Calling comparefn(a,b) does not modify obj or any object on obj’s prototype chain.

  • a =CF a (reflexivity)

  • If a =CF b, then b =CF a (symmetry)

  • If a =CF b and b =CF c, then a =CF c (transitivity of =CF)

  • If a <CF b and b <CF c, then a <CF c (transitivity of <CF)

  • If a >CF b and b >CF c, then a >CF c (transitivity of >CF)

NOTE 1 The above conditions are necessary and sufficient to ensure that comparefn divides the set S into equivalence classes and that these equivalence classes are totally ordered.

NOTE 2 The sort function is intentionally generic; it does not require that its this value be an Array object. Therefore, it can be transferred to other kinds of objects for use as a method.

22.1.3.24.1 Runtime Semantics: SortCompare( x, y )

The SortCompare abstract operation is called with two arguments x and y. It also has access to the comparefn argument passed to the current invocation of the sort method. The following steps are taken:

  1. If x and y are both undefined, return +0.
  2. If x is undefined, return 1.
  3. If y is undefined, return −1.
  4. If the argument comparefn is not undefined, then
    1. Let v be ToNumber(Call(comparefn, undefined, «x, y»)).
    2. ReturnIfAbrupt(v).
    3. If v is NaN, return +0.
    4. Return v.
  5. Let xString be ToString(x).
  6. ReturnIfAbrupt(xString).
  7. Let yString be ToString(y).
  8. ReturnIfAbrupt(yString).
  9. If xString < yString, return −1.
  10. If xString > yString, return 1.
  11. Return +0.

NOTE 1 Because non-existent property values always compare greater than undefined property values, and undefined always compares greater than any other value, undefined property values always sort to the end of the result, followed by non-existent property values.

NOTE 2 Method calls performed by the ToString abstract operations in steps 5 and 7 have the potential to cause SortCompare to not behave as a consistent comparison function.

22.1.3.25 Array.prototype.splice (start, deleteCount , ...items )

NOTE 1 When the splice method is called with two or more arguments start, deleteCount and zero or more items, the deleteCount elements of the array starting at integer index start are replaced by the arguments items. An Array object containing the deleted elements (if any) is returned.

The following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. Let relativeStart be ToInteger(start).
  6. ReturnIfAbrupt(relativeStart).
  7. If relativeStart < 0, let actualStart be max((len + relativeStart),0); else let actualStart be min(relativeStart, len).
  8. If the number of actual arguments is 0, then
    1. Let insertCount be 0.
    2. Let actualDeleteCount be 0.
  9. Else if the number of actual arguments is 1, then
    1. Let insertCount be 0.
    2. Let actualDeleteCount be lenactualStart.
  10. Else,
    1. Let insertCount be the number of actual arguments minus 2.
    2. Let dc be ToInteger(deleteCount).
    3. ReturnIfAbrupt(dc).
    4. Let actualDeleteCount be min(max(dc,0), lenactualStart).
  11. If len+insertCountactualDeleteCount > 253-1, throw a TypeError exception.
  12. Let A be ArraySpeciesCreate(O, actualDeleteCount).
  13. ReturnIfAbrupt(A).
  14. Let k be 0.
  15. Repeat, while k < actualDeleteCount
    1. Let from be ToString(actualStart+k).
    2. Let fromPresent be HasProperty(O, from).
    3. ReturnIfAbrupt(fromPresent).
    4. If fromPresent is true, then
      1. Let fromValue be Get(O, from).
      2. ReturnIfAbrupt(fromValue).
      3. Let status be CreateDataPropertyOrThrow(A, ToString(k), fromValue).
      4. ReturnIfAbrupt(status).
    5. Increment k by 1.
  16. Let setStatus be Set(A, "length", actualDeleteCount, true).
  17. ReturnIfAbrupt(setStatus).
  18. Let items be a List whose elements are, in left to right order, the portion of the actual argument list starting with the third argument. The list is empty if fewer than three arguments were passed.
  19. Let itemCount be the number of elements in items.
  20. If itemCount < actualDeleteCount, then
    1. Let k be actualStart.
    2. Repeat, while k < (lenactualDeleteCount)
      1. Let from be ToString(k+actualDeleteCount).
      2. Let to be ToString(k+itemCount).
      3. Let fromPresent be HasProperty(O, from).
      4. ReturnIfAbrupt(fromPresent).
      5. If fromPresent is true, then
        1. Let fromValue be Get(O, from).
        2. ReturnIfAbrupt(fromValue).
        3. Let setStatus be Set(O, to, fromValue, true).
        4. ReturnIfAbrupt(setStatus).
      6. Else fromPresent is false,
        1. Let deleteStatus be DeletePropertyOrThrow(O, to).
        2. ReturnIfAbrupt(deleteStatus).
      7. Increase k by 1.
    3. Let k be len.
    4. Repeat, while k > (lenactualDeleteCount + itemCount)
      1. Let deleteStatus be DeletePropertyOrThrow(O, ToString(k–1)).
      2. ReturnIfAbrupt(deleteStatus).
      3. Decrease k by 1.
  21. Else if itemCount > actualDeleteCount, then
    1. Let k be (lenactualDeleteCount).
    2. Repeat, while k > actualStart
      1. Let from be ToString(k + actualDeleteCount – 1).
      2. Let to be ToString(k + itemCount – 1)
      3. Let fromPresent be HasProperty(O, from).
      4. ReturnIfAbrupt(fromPresent).
      5. If fromPresent is true, then
        1. Let fromValue be Get(O, from).
        2. ReturnIfAbrupt(fromValue).
        3. Let setStatus be Set(O, to, fromValue, true).
        4. ReturnIfAbrupt(setStatus).
      6. Else fromPresent is false,
        1. Let deleteStatus be DeletePropertyOrThrow(O, to).
        2. ReturnIfAbrupt(deleteStatus).
      7. Decrease k by 1.
  22. Let k be actualStart.
  23. Repeat, while items is not empty
    1. Remove the first element from items and let E be the value of that element.
    2. Let setStatus be Set(O, ToString(k), E, true).
    3. ReturnIfAbrupt(setStatus).
    4. Increase k by 1.
  24. Let setStatus be Set(O, "length", lenactualDeleteCount + itemCount, true).
  25. ReturnIfAbrupt(setStatus).
  26. Return A.

The length property of the splice method is 2.

NOTE 2 The explicit setting of the length property of the result Array in step 24 is necessary to ensure that its value is correct in situations where its trailing elements are not present.

NOTE 3 The splice function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.26 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )

An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement the Array.prototype.toLocaleString method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of the toLocaleString method is used.

NOTE 1 The first edition of ECMA-402 did not include a replacement specification for the Array.prototype.toLocaleString method.

The meanings of the optional parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.

The following steps are taken:

  1. Let array be ToObject(this value).
  2. ReturnIfAbrupt(array).
  3. Let len be ToLength(Get(array, "length")).
  4. ReturnIfAbrupt(len).
  5. Let separator be the String value for the list-separator String appropriate for the host environment’s current locale (this is derived in an implementation-defined way).
  6. If len is zero, return the empty String.
  7. Let firstElement be Get(array, "0").
  8. ReturnIfAbrupt(firstElement).
  9. If firstElement is undefined or null, then
    1. Let R be the empty String.
  10. Else
    1. Let R be ToString(Invoke(firstElement, "toLocaleString")).
    2. ReturnIfAbrupt(R).
  11. Let k be 1.
  12. Repeat, while k < len
    1. Let S be a String value produced by concatenating R and separator.
    2. Let nextElement be Get(array, ToString(k)).
    3. ReturnIfAbrupt(nextElement).
    4. If nextElement is undefined or null, then
      1. Let R be the empty String.
    5. Else
      1. Let R be ToString(Invoke(nextElement, "toLocaleString")).
      2. ReturnIfAbrupt(R).
    6. Let R be a String value produced by concatenating S and R.
    7. Increase k by 1.
  13. Return R.

NOTE 2 The elements of the array are converted to Strings using their toLocaleString methods, and these Strings are then concatenated, separated by occurrences of a separator String that has been derived in an implementation-defined locale-specific way. The result of calling this function is intended to be analogous to the result of toString, except that the result of this function is intended to be locale-specific.

NOTE 3 The toLocaleString function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.27 Array.prototype.toString ( )

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

  1. Let array be ToObject(this value).
  2. ReturnIfAbrupt(array).
  3. Let func be Get(array, "join").
  4. ReturnIfAbrupt(func).
  5. If IsCallable(func) is false, let func be the intrinsic function %ObjProto_toString% (19.1.3.6).
  6. Return Call(func, array).

NOTE The toString function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.28 Array.prototype.unshift ( ...items )

NOTE 1 The arguments are prepended to the start of the array, such that their order within the array is the same as the order in which they appear in the argument list.

When the unshift method is called with zero or more arguments item1, item2, etc., the following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Let len be ToLength(Get(O, "length")).
  4. ReturnIfAbrupt(len).
  5. Let argCount be the number of actual arguments.
  6. If argCount > 0, then
    1. If len+ argCount > 253-1, throw a TypeError exception.
    2. Let k be len.
    3. Repeat, while k > 0,
      1. Let from be ToString(k–1).
      2. Let to be ToString(k+argCount –1).
      3. Let fromPresent be HasProperty(O, from).
      4. ReturnIfAbrupt(fromPresent).
      5. If fromPresent is true, then
        1. Let fromValue be Get(O, from).
        2. ReturnIfAbrupt(fromValue).
        3. Let setStatus be Set(O, to, fromValue, true).
        4. ReturnIfAbrupt(setStatus).
      6. Else fromPresent is false,
        1. Let deleteStatus be DeletePropertyOrThrow(O, to).
        2. ReturnIfAbrupt(deleteStatus).
      7. Decrease k by 1.
    4. Let j be 0.
    5. Let items be a List whose elements are, in left to right order, the arguments that were passed to this function invocation.
    6. Repeat, while items is not empty
      1. Remove the first element from items and let E be the value of that element.
      2. Let setStatus be Set(O, ToString(j), E, true).
      3. ReturnIfAbrupt(setStatus).
      4. Increase j by 1.
  7. Let setStatus be Set(O, "length", len+argCount, true).
  8. ReturnIfAbrupt(setStatus).
  9. Return len+argCount.

The length property of the unshift method is 1.

NOTE 2 The unshift function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

22.1.3.29 Array.prototype.values ( )

The following steps are taken:

  1. Let O be ToObject(this value).
  2. ReturnIfAbrupt(O).
  3. Return CreateArrayIterator(O, "value").

This function is the %ArrayProto_values% intrinsic object.

22.1.3.30 Array.prototype [ @@iterator ] ( )

The initial value of the @@iterator property is the same function object as the initial value of the Array.prototype.values property.

22.1.3.31 Array.prototype [ @@unscopables ]

The initial value of the @@unscopables data property is an object created by the following steps:

  1. Let blackList be ObjectCreate(null).
  2. Perform CreateDataProperty(blackList, "copyWithin", true).
  3. Perform CreateDataProperty(blackList, "entries", true).
  4. Perform CreateDataProperty(blackList, "fill", true).
  5. Perform CreateDataProperty(blackList, "find", true).
  6. Perform CreateDataProperty(blackList, "findIndex", true).
  7. Perform CreateDataProperty(blackList, "keys", true).
  8. Perform CreateDataProperty(blackList, "values", true).
  9. Assert: Each of the above calls will return true.
  10. Return blackList.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.

NOTE The own property names of this object are property names that were not included as standard properties of Array.prototype prior to the ECMAScript 2015 specification. These names are ignored for with statement binding purposes in order to preserve the behaviour of existing code that might use one of these names as a binding in an outer scope that is shadowed by a with statement whose binding object is an Array object.

22.1.4 Properties of Array Instances

Array instances are Array exotic objects and have the internal methods specified for such objects. Array instances inherit properties from the Array prototype object.

Array instances have a length property, and a set of enumerable properties with array index names.

22.1.4.1 length

The length property of an Array instance is a data property whose value is always numerically greater than the name of every configurable own property whose name is an array index.

The length property initially has the attributes { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }.

NOTE Reducing the value of the length property has the side-effect of deleting own array elements whose array index is between the old and new length values. However, non-configurable properties can not be deleted. Attempting to set the length property of an Array object to a value that is numerically less than or equal to the largest numeric own property name of an existing non-configurable array indexed property of the array will result in the length being set to a numeric value that is one greater than that non-configurable numeric own property name. See 9.4.2.1.

22.1.5 Array Iterator Objects

An Array Iterator is an object, that represents a specific iteration over some specific Array instance object. There is not a named constructor for Array Iterator objects. Instead, Array iterator objects are created by calling certain methods of Array instance objects.

22.1.5.1 CreateArrayIterator Abstract Operation

Several methods of Array objects return Iterator objects. The abstract operation CreateArrayIterator with arguments array and kind is used to create such iterator objects. It performs the following steps:

  1. Assert: Type(array) is Object.
  2. Let iterator be ObjectCreate(%ArrayIteratorPrototype%, «‍[[IteratedObject]], [[ArrayIteratorNextIndex]], [[ArrayIterationKind]]»).
  3. Set iterator’s [[IteratedObject]] internal slot to array.
  4. Set iterator’s [[ArrayIteratorNextIndex]] internal slot to 0.
  5. Set iterator’s [[ArrayIterationKind]] internal slot to kind.
  6. Return iterator.

22.1.5.2 The %ArrayIteratorPrototype% Object

All Array Iterator Objects inherit properties from the %ArrayIteratorPrototype% intrinsic object. The %ArrayIteratorPrototype% object is an ordinary object and its [[Prototype]] internal slot is the %IteratorPrototype% intrinsic object (25.1.2). In addition, %ArrayIteratorPrototype% has the following properties:

22.1.5.2.1 %ArrayIteratorPrototype%.next( )

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have all of the internal slots of an Array Iterator Instance (22.1.5.3), throw a TypeError exception.
  4. Let a be the value of the [[IteratedObject]] internal slot of O.
  5. If a is undefined, return CreateIterResultObject(undefined, true).
  6. Let index be the value of the [[ArrayIteratorNextIndex]] internal slot of O.
  7. Let itemKind be the value of the [[ArrayIterationKind]] internal slot of O.
  8. If a has a [[TypedArrayName]] internal slot, then
    1. Let len be the value of O's [[ArrayLength]] internal slot.
  9. Else,
    1. Let len be ToLength(Get(a, "length")).
    2. ReturnIfAbrupt(len).
  10. If indexlen, then
    1. Set the value of the [[IteratedObject]] internal slot of O to undefined.
    2. Return CreateIterResultObject(undefined, true).
  11. Set the value of the [[ArrayIteratorNextIndex]] internal slot of O to index+1.
  12. If itemKind is "key", return CreateIterResultObject(index, false).
  13. Let elementKey be ToString(index).
  14. Let elementValue be Get(a, elementKey).
  15. ReturnIfAbrupt(elementValue).
  16. If itemKind is "value", let result be elementValue.
  17. Else,
    1. Assert: itemKind is "key+value".
    2. Let result be CreateArrayFromListindex, elementValue»).
  18. Return CreateIterResultObject(result, false).

22.1.5.2.2 %ArrayIteratorPrototype% [ @@toStringTag ]

The initial value of the @@toStringTag property is the String value "Array Iterator".

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.

22.1.5.3 Properties of Array Iterator Instances

Array Iterator instances are ordinary objects that inherit properties from the %ArrayIteratorPrototype% intrinsic object. Array Iterator instances are initially created with the internal slots listed in Table 48.

Table 48 — Internal Slots of Array Iterator Instances
Internal Slot Description
[[IteratedObject]] The object whose array elements are being iterated.
[[ArrayIteratorNextIndex]] The integer index of the next integer index to be examined by this iteration.
[[ArrayIterationKind]] A String value that identifies what is returned for each element of the iteration. The possible values are: "key", "value", "key+value".

22.2 TypedArray Objects

TypedArray objects present an array-like view of an underlying binary data buffer (24.1). Each element of a TypedArray instance has the same underlying binary scalar data type. There is a distinct TypedArray constructor, listed in Table 49, for each of the nine supported element types. Each constructor in Table 49 has a corresponding distinct prototype object.

Table 49 – The TypedArray Constructors
Constructor Name and Intrinsic Element Type Element Size Conversion Operation Description Equivalent C Type
Int8Array
%Int8Array%
Int8 1 ToInt8 8-bit 2's complement signed integer signed char
Uint8Array
%Uint8Array%
Uint8 1 ToUint8 8-bit unsigned integer unsigned char
Uint8ClampedArray
%Uint8ClampedArray%
Uint8C 1 ToUint8Clamp 8-bit unsigned integer (clamped conversion) unsigned char
Int16Array
%Int16Array%
Int16 2 ToInt16 16-bit 2's complement signed integer short
Uint16Array
%Uint16Array%
Uint16 2 ToUint16 16-bit unsigned integer unsigned short
Int32Array
%Int32Array%
Int32 4 ToInt32 32-bit 2's complement signed integer int
Uint32Array
%Uint32Array%
Uint32 4 ToUint32 32-bit unsigned integer unsigned int
Float32Array
%Float32Array%
Float32 4 32-bit IEEE floating point float
Float64Array
%Float64Array%
Float64 8 64-bit IEEE floating point double

In the definitions below, references to TypedArray should be replaced with the appropriate constructor name from the above table. The phrase “the element size in bytes” refers to the value in the Element Size column of the table in the row corresponding to the constructor. The phrase “element Type” refers to the value in the Element Type column for that row.

22.2.1 The %TypedArray% Intrinsic Object

The %TypedArray% intrinsic object is a constructor function object that all of the TypedArray constructor object inherit from. %TypedArray% and its corresponding prototype object provide common properties that are inherited by all TypedArray constructors and their instances. The %TypedArray% intrinsic does not have a global name or appear as a property of the global object.

The %TypedArray% intrinsic function object is designed to act as the superclass of the various TypedArray constructors. Those constructors use %TypedArray% to initialize their instances by invoking %TypedArray% as if by making a super call. The %TypedArray% intrinsic function is not designed to be directly called in any other way. If %TypedArray% is directly called or called as part of a new expression an exception is thrown.

The %TypedArray% intrinsic constructor function is a single function whose behaviour is overloaded based upon the number and types of its arguments. The actual behaviour of a super call of %TypedArray% depends upon the number and kind of arguments that are passed to it.

22.2.1.1 %TypedArray% ( )

This description applies only if the %TypedArray% function is called with no arguments.

  1. If NewTarget is undefined, throw a TypeError exception.
  2. Return AllocateTypedArray(NewTarget, 0).

22.2.1.2 %TypedArray% ( length )

This description applies only if the %TypedArray% function is called with at least one argument and the Type of the first argument is not Object.

%TypedArray% called with argument length performs the following steps:

  1. Assert: Type(length) is not Object.
  2. If NewTarget is undefined, throw a TypeError exception.
  3. If length is undefined, throw a TypeError exception.
  4. Let numberLength be ToNumber(length).
  5. Let elementLength be ToLength(numberLength).
  6. ReturnIfAbrupt(elementLength).
  7. If SameValueZero(numberLength, elementLength) is false, throw a RangeError exception.
  8. Return AllocateTypedArray(NewTarget, elementLength).

22.2.1.2.1 Runtime Semantics: AllocateTypedArray (newTarget, length )

The abstract operation AllocateTypedArray with argument newTarget and optional argument length is used to validate and create an instance of a TypedArray constructor. If the length argument is passed an ArrayBuffer of that length is also allocated and associated with the new TypedArray instance. AllocateTypedArray provides common semantics that is used by all of the %TypeArray% overloads and other methods. AllocateTypedArray performs the following steps:

  1. Assert: IsConstructor(newTarget) is true.
  2. If SameValue(%TypedArray%, newTarget) is true, throw a TypeError exception.
  3. NOTE %TypedArray% throws an exception when invoked via either a function call or the new operator. It can only be successfully invoked by a SuperCall.
  4. Let constructorName be undefined.
  5. Let subclass be newTarget.
  6. Repeat while constructorName is undefined
    1. If subclass is null, throw a TypeError exception.
    2. If SameValue(%TypedArray%, subclass) is true, throw a TypeError exception.
    3. If subclass has a [[TypedArrayConstructorName]] internal slot, let constructorName be the value of subclass's [[TypedArrayConstructorName]] internal slot.
    4. Let subclass be subclass.[[GetPrototypeOf]]().
    5. ReturnIfAbrupt(subclass).
  7. Let proto be GetPrototypeFromConstructor(newTarget, "%TypedArrayPrototype%").
  8. ReturnIfAbrupt(proto).
  9. Let obj be IntegerIndexedObjectCreate (proto, «‍[[ViewedArrayBuffer]], [[TypedArrayName]], [[ByteLength]], [[ByteOffset]], [[ArrayLength]]» ).
  10. Assert: The [[ViewedArrayBuffer]] internal slot of obj is undefined.
  11. Set obj's [[TypedArrayName]] internal slot to constructorName.
  12. If length was not passed, then
    1. Set obj's [[ByteLength]] internal slot to 0.
    2. Set obj's [[ByteOffset]] internal slot to 0.
    3. Set obj's [[ArrayLength]] internal slot to 0.
  13. Else,
    1. Let elementSize be the Element Size value in Table 49 for constructorName.
    2. Let byteLength be elementSize × length.
    3. Let data be AllocateArrayBuffer(%ArrayBuffer%, byteLength).
    4. ReturnIfAbrupt(data).
    5. Set obj’s [[ViewedArrayBuffer]] internal slot to data.
    6. Set obj's [[ByteLength]] internal slot to byteLength.
    7. Set obj's [[ByteOffset]] internal slot to 0.
    8. Set obj's [[ArrayLength]] internal slot to length.
  14. Return obj.

22.2.1.3 %TypedArray% ( typedArray )

This description applies only if the %TypedArray% function is called with at least one argument and the Type of the first argument is Object and that object has a [[TypedArrayName]] internal slot.

%TypedArray% called with argument typedArray performs the following steps:

  1. Assert: Type(typedArray) is Object and typedArray has a [[TypedArrayName]] internal slot.
  2. If NewTarget is undefined, throw a TypeError exception.
  3. Let O be AllocateTypedArray(NewTarget).
  4. ReturnIfAbrupt(O).
  5. Let srcArray be typedArray.
  6. Let srcData be the value of srcArray’s [[ViewedArrayBuffer]] internal slot.
  7. If IsDetachedBuffer(srcData) is true, throw a TypeError exception.
  8. Let constructorName be the String value of O's [[TypedArrayName]] internal slot.
  9. Let elementType be the String value of the Element Type value in Table 49 for constructorName.
  10. Let elementLength be the value of srcArray’s [[ArrayLength]] internal slot.
  11. Let srcName be the String value of srcArray’s [[TypedArrayName]] internal slot.
  12. Let srcType be the String value of the Element Type value in Table 49 for srcName.
  13. Let srcElementSize be the Element Size value in Table 49 for srcName.
  14. Let srcByteOffset be the value of srcArray's [[ByteOffset]] internal slot.
  15. Let elementSize be the Element Size value in Table 49 for constructorName.
  16. Let byteLength be elementSize × elementLength.
  17. If SameValue(elementType,srcType) is true, then
    1. Let data be CloneArrayBuffer(srcData, srcByteOffset).
    2. ReturnIfAbrupt(data).
  18. Else,
    1. Let bufferConstructor be SpeciesConstructor(srcData, %ArrayBuffer%).
    2. ReturnIfAbrupt(bufferConstructor).
    3. Let data be AllocateArrayBuffer(bufferConstructor, byteLength).
    4. ReturnIfAbrupt(data).
    5. If IsDetachedBuffer(srcData) is true, throw a TypeError exception.
    6. Let srcByteIndex be srcByteOffset.
    7. Let targetByteIndex be 0.
    8. Let count be elementLength.
    9. Repeat, while count >0
      1. Let value be GetValueFromBuffer(srcData, srcByteIndex, srcType).
      2. Perform SetValueInBuffer(data, targetByteIndex, elementType, value).
      3. Set srcByteIndex to srcByteIndex + srcElementSize.
      4. Set targetByteIndex to targetByteIndex + elementSize.
      5. Decrement count by 1.
  19. Set O’s [[ViewedArrayBuffer]] internal slot to data.
  20. Set O's [[ByteLength]] internal slot to byteLength.
  21. Set O's [[ByteOffset]] internal slot to 0.
  22. Set O's [[ArrayLength]] internal slot to elementLength.
  23. Return O.

22.2.1.4 %TypedArray% ( object )

This description applies only if the %TypedArray% function is called with at least one argument and the Type of the first argument is Object and that object does not have either a [[TypedArrayName]] or an [[ArrayBufferData]] internal slot.

%TypedArray% called with argument object performs the following steps:

  1. Assert: Type(object) is Object and object does not have either a [[TypedArrayName]] or an [[ArrayBufferData]] internal slot.
  2. If NewTarget is undefined, throw a TypeError exception.
  3. Return TypedArrayFrom(NewTarget, object, undefined, undefined).

22.2.1.5 %TypedArray% ( buffer [ , byteOffset [ , length ] ] )

This description applies only if the %TypedArray% function is called with at least one argument and the Type of the first argument is Object and that object has an [[ArrayBufferData]] internal slot.

%TypedArray% called with arguments buffer, byteOffset, and length performs the following steps:

  1. Assert: Type(buffer) is Object and buffer has an [[ArrayBufferData]] internal slot.
  2. If NewTarget is undefined, throw a TypeError exception.
  3. Let O be AllocateTypedArray(NewTarget).
  4. ReturnIfAbrupt(O).
  5. Let constructorName be the String value of O's [[TypedArrayName]] internal slot.
  6. Let elementSize be the Number value of the Element Size value in Table 49 for constructorName.
  7. Let offset be ToInteger(byteOffset).
  8. ReturnIfAbrupt(offset).
  9. If offset < 0, throw a RangeError exception.
  10. If offset modulo elementSize ≠ 0, throw a RangeError exception.
  11. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  12. Let bufferByteLength be the value of buffer’s [[ArrayBufferByteLength]] internal slot.
  13. If length is undefined, then
    1. If bufferByteLength modulo elementSize ≠ 0, throw a RangeError exception.
    2. Let newByteLength be bufferByteLengthoffset.
    3. If newByteLength < 0, throw a RangeError exception.
  14. Else,
    1. Let newLength be ToLength(length).
    2. ReturnIfAbrupt(newLength).
    3. Let newByteLength be newLength × elementSize.
    4. If offset+newByteLength > bufferByteLength, throw a RangeError exception.
  15. Set O’s [[ViewedArrayBuffer]] internal slot to buffer.
  16. Set O's [[ByteLength]] internal slot to newByteLength.
  17. Set O's [[ByteOffset]] internal slot to offset.
  18. Set O's [[ArrayLength]] internal slot to newByteLength / elementSize .
  19. Return O.

22.2.2 Properties of the %TypedArray% Intrinsic Object

The value of the [[Prototype]] internal slot of %TypedArray% is the intrinsic object %FunctionPrototype% (19.2.3).

Besides a length property whose value is 3 and a name property whose value is "TypedArray", %TypedArray% has the following properties:

22.2.2.1 %TypedArray%.from ( source [ , mapfn [ , thisArg ] ] )

When the from method is called with argument source, and optional arguments mapfn and thisArg, the following steps are taken:

  1. Let C be the this value.
  2. If IsConstructor(C) is false, throw a TypeError exception.
  3. If mapfn was supplied, let f be mapfn; otherwise let f be undefined.
  4. If f is not undefined, then
    1. If IsCallable(f) is false, throw a TypeError exception.
  5. If thisArg was supplied, let t be thisArg; else let t be undefined.
  6. Return TypedArrayFrom(C, source, f, t).

The length property of the from method is 1.

22.2.2.1.1 Runtime Semantics: TypedArrayFrom( constructor, items, mapfn, thisArg )

When the TypedArrayFrom abstract operation is called with arguments constructor, items, mapfn, and thisArg, the following steps are taken:

  1. Let C be constructor.
  2. Assert: IsConstructor(C) is true.
  3. Assert: mapfn is either a callable Object or undefined.
  4. If mapfn is undefined, let mapping be false.
  5. Else
    1. Let T be thisArg.
    2. Let mapping be true
  6. Let usingIterator be GetMethod(items, @@iterator).
  7. ReturnIfAbrupt(usingIterator).
  8. If usingIterator is not undefined, then
    1. Let iterator be GetIterator(items, usingIterator).
    2. ReturnIfAbrupt(iterator).
    3. Let values be a new empty List.
    4. Let next be true.
    5. Repeat, while next is not false
      1. Let next be IteratorStep(iterator).
      2. ReturnIfAbrupt(next).
      3. If next is not false, then
        1. Let nextValue be IteratorValue(next).
        2. ReturnIfAbrupt(nextValue).
        3. Append nextValue to the end of the List values.
    6. Let len be the number of elements in values.
    7. Let targetObj be AllocateTypedArray(C, len).
    8. ReturnIfAbrupt(targetObj).
    9. Let k be 0.
    10. Repeat, while k < len
      1. Let Pk be ToString(k).
      2. Let kValue be the first element of values and remove that element from values.
      3. If mapping is true, then
        1. Let mappedValue be Call(mapfn, T, «kValue, k»).
        2. ReturnIfAbrupt(mappedValue).
      4. Else, let mappedValue be kValue.
      5. Let setStatus be Set(targetObj, Pk, mappedValue, true).
      6. ReturnIfAbrupt(setStatus).
      7. Increase k by 1.
    11. Assert: values is now an empty List.
    12. Return targetObj.
  9. Assert: items is not an Iterable so assume it is an array-like object.
  10. Let arrayLike be ToObject(items).
  11. ReturnIfAbrupt(arrayLike).
  12. Let len be ToLength(Get(arrayLike, "length")).
  13. ReturnIfAbrupt(len).
  14. Let targetObj be AllocateTypedArray(C, len).
  15. ReturnIfAbrupt(targetObj).
  16. Let k be 0.
  17. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kValue be Get(arrayLike, Pk).
    3. ReturnIfAbrupt(kValue).
    4. If mapping is true, then
      1. Let mappedValue be Call(mapfn, T, «kValue, k»).
      2. ReturnIfAbrupt(mappedValue).
    5. Else, let mappedValue be kValue.
    6. Let setStatus be Set(targetObj, Pk, mappedValue, true).
    7. ReturnIfAbrupt(setStatus).
    8. Increase k by 1.
  18. Return targetObj.

22.2.2.2 %TypedArray%.of ( ...items )

When the of method is called with any number of arguments, the following steps are taken:

  1. Let len be the actual number of arguments passed to this function.
  2. Let items be the List of arguments passed to this function.
  3. Let C be the this value.
  4. If IsConstructor(C) is false, throw a TypeError exception.
  5. Let newObj be AllocateTypedArray(C, len).
  6. ReturnIfAbrupt(newObj).
  7. Let k be 0.
  8. Repeat, while k < len
    1. Let kValue be items[k].
    2. Let Pk be ToString(k).
    3. Let status be Set(newObj, Pk, kValue, true).
    4. ReturnIfAbrupt(status).
    5. Increase k by 1.
  9. Return newObj.

The length property of the of method is 0.

NOTE The items argument is assumed to be a well-formed rest argument value.

22.2.2.3 %TypedArray%.prototype

The initial value of %TypedArray%.prototype is the %TypedArrayPrototype% intrinsic object (22.2.3).

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

22.2.2.4 get %TypedArray% [ @@species ]

%TypedArray%[@@species] is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Return the this value.

The value of the name property of this function is "get [Symbol.species]".

NOTE %TypedArrayPrototype% methods normally use their this object’s constructor to create a derived object. However, a subclass constructor may over-ride that default behaviour by redefining its @@species property.

22.2.3 Properties of the %TypedArrayPrototype% Object

The value of the [[Prototype]] internal slot of the %TypedArrayPrototype% object is the intrinsic object %ObjectPrototype% (19.1.3). The %TypedArrayPrototype% object is an ordinary object. It does not have a [[ViewedArrayBuffer]] or any other of the internal slots that are specific to TypedArray instance objects.

22.2.3.1 get %TypedArray%.prototype.buffer

%TypedArray%.prototype.buffer is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have a [[ViewedArrayBuffer]] internal slot, throw a TypeError exception.
  4. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
  5. Return buffer.

22.2.3.2 get %TypedArray%.prototype.byteLength

%TypedArray%.prototype.byteLength is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have a [[ViewedArrayBuffer]] internal slot, throw a TypeError exception.
  4. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
  5. If IsDetachedBuffer(buffer) is true, return 0.
  6. Let size be the value of O's [[ByteLength]] internal slot.
  7. Return size.

22.2.3.3 get %TypedArray%.prototype.byteOffset

%TypedArray%.prototype.byteOffset is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have a [[ViewedArrayBuffer]] internal slot, throw a TypeError exception.
  4. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
  5. If IsDetachedBuffer(buffer) is true, return 0.
  6. Let offset be the value of O's [[ByteOffset]] internal slot.
  7. Return offset.

22.2.3.4 %TypedArray%.prototype.constructor

The initial value of %TypedArray%.prototype.constructor is the %TypedArray% intrinsic object.

22.2.3.5 %TypedArray%.prototype.copyWithin (target, start [, end ] )

%TypedArray%.prototype.copyWithin is a distinct function that implements the same algorithm as Array.prototype.copyWithin as defined in 22.1.3.3 except that the this object's [[ArrayLength]] internal slot is accessed in place of performing a [[Get]] of "length" and the actual copying of values in step 17 must be performed in a manner that preserves the bit-level encoding of the source data

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. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the copyWithin method is 2.

22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O )

When called with argument O the following steps are taken:

  1. If Type(O) is not Object, throw a TypeError exception.
  2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError exception.
  3. If O does not have a [[ViewedArrayBuffer]] internal slot, throw a TypeError exception.
  4. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
  5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  6. Return buffer.

22.2.3.6 %TypedArray%.prototype.entries ( )

The following steps are taken:

  1. Let O be the this value.
  2. Let valid be ValidateTypedArray(O).
  3. ReturnIfAbrupt(valid).
  4. Return CreateArrayIterator(O, "key+value").

22.2.3.7 %TypedArray%.prototype.every ( callbackfn [ , thisArg ] )

%TypedArray%.prototype.every is a distinct function that implements the same algorithm as Array.prototype.every as defined in 22.1.3.5 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 and must take into account the possibility that calls to callbackfn may cause the this value to become detached.

This function is not generic. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the every method is 1.

22.2.3.8 %TypedArray%.prototype.fill (value [ , start [ , end ] ] )

%TypedArray%.prototype.fill is a distinct function that implements the same algorithm as Array.prototype.fill as defined in 22.1.3.6 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. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the fill method is 1.

22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.filter are the same as for Array.prototype.filter as defined in 22.1.3.7.

When the filter method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Let valid be ValidateTypedArray(O).
  3. ReturnIfAbrupt(valid).
  4. Let len be the value of O’s [[ArrayLength]] internal slot.
  5. If IsCallable(callbackfn) is false, throw a TypeError exception.
  6. If thisArg was supplied, let T be thisArg; else let T be undefined.
  7. Let defaultConstructor be the intrinsic object listed in column one of Table 49 for the value of O's [[TypedArrayName]] internal slot.
  8. Let C be SpeciesConstructor(O, defaultConstructor).
  9. ReturnIfAbrupt(C).
  10. Let kept be a new empty List.
  11. Let k be 0.
  12. Let captured be 0.
  13. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kValue be Get(O, Pk).
    3. ReturnIfAbrupt(kValue).
    4. Let selected be ToBoolean(Call(callbackfn, T, «kValue, k, O»)).
    5. ReturnIfAbrupt(selected).
    6. If selected is true, then
      1. Append kValue to the end of kept.
      2. Increase captured by 1.
    7. Increase k by 1.
  14. Let A be AllocateTypedArray(C, captured).
  15. ReturnIfAbrupt(A).
  16. Let n be 0.
  17. For each element e of kept
    1. Let status be Set(A, ToString(n), e, true ).
    2. ReturnIfAbrupt(status).
    3. Increment n by 1.
  18. Return A.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

The length property of the filter method is 1.

22.2.3.10 %TypedArray%.prototype.find (predicate [ , thisArg ] )

%TypedArray%.prototype.find is a distinct function that implements the same algorithm as Array.prototype.find as defined in 22.1.3.8 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 and must take into account the possibility that calls to predicate may cause the this value to become detached.

This function is not generic. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the find method is 1.

22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )

%TypedArray%.prototype.findIndex is a distinct function that implements the same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 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 and must take into account the possibility that calls to predicate may cause the this value to become detached.

This function is not generic. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the findIndex method is 1.

22.2.3.12 %TypedArray%.prototype.forEach ( callbackfn [ , thisArg ] )

%TypedArray%.prototype.forEach is a distinct function that implements the same algorithm as Array.prototype.forEach as defined in 22.1.3.10 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 and must take into account the possibility that calls to callbackfn may cause the this value to become detached.

This function is not generic. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the forEach method is 1.

22.2.3.13 %TypedArray%.prototype.indexOf (searchElement [ , fromIndex ] )

%TypedArray%.prototype.indexOf is a distinct function that implements the same algorithm as Array.prototype.indexOf as defined in 22.1.3.11 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. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the indexOf method is 1.

22.2.3.14 %TypedArray%.prototype.join ( separator )

%TypedArray%.prototype.join is a distinct function that implements the same algorithm as Array.prototype.join as defined in 22.1.3.12 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. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

22.2.3.15 %TypedArray%.prototype.keys ( )

The following steps are taken:

  1. Let O be the this value.
  2. Let valid be ValidateTypedArray(O).
  3. ReturnIfAbrupt(valid).
  4. Return CreateArrayIterator(O, "key").

22.2.3.16 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] )

%TypedArray%.prototype.lastIndexOf is a distinct function that implements the same algorithm as Array.prototype.lastIndexOf as defined in 22.1.3.14 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. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the lastIndexOf method is 1.

22.2.3.17 get %TypedArray%.prototype.length

%TypedArray%.prototype.length is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError exception.
  4. Assert: O has [[ViewedArrayBuffer]] and [[ArrayLength]] internal slots.
  5. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
  6. If IsDetachedBuffer(buffer) is true, return 0.
  7. Let length be the value of O's [[ArrayLength]] internal slot.
  8. Return length.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

22.2.3.18 %TypedArray%.prototype.map ( callbackfn [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.map are the same as for Array.prototype.map as defined in 22.1.3.15.

When the map method is called with one or two arguments, the following steps are taken:

  1. Let O be the this value.
  2. Let valid be ValidateTypedArray(O).
  3. ReturnIfAbrupt(valid).
  4. Let len be the value of O’s [[ArrayLength]] internal slot.
  5. If IsCallable(callbackfn) is false, throw a TypeError exception.
  6. If thisArg was supplied, let T be thisArg; else let T be undefined.
  7. Let defaultConstructor be the intrinsic object listed in column one of Table 49 for the value of O's [[TypedArrayName]] internal slot.
  8. Let C be SpeciesConstructor(O, defaultConstructor).
  9. ReturnIfAbrupt(C).
  10. Let A be AllocateTypedArray(C, len).
  11. ReturnIfAbrupt(A).
  12. Let k be 0.
  13. Repeat, while k < len
    1. Let Pk be ToString(k).
    2. Let kValue be Get(O, Pk).
    3. ReturnIfAbrupt(kValue).
    4. Let mappedValue be Call(callbackfn, T, «kValue, k, O»).
    5. ReturnIfAbrupt(mappedValue).
    6. Let status be Set(A, Pk, mappedValue, true ).
    7. ReturnIfAbrupt(status).
    8. Increase k by 1.
  14. Return A.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

The length property of the map method is 1.

22.2.3.19 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] )

%TypedArray%.prototype.reduce is a distinct function that implements the same algorithm as Array.prototype.reduce as defined in 22.1.3.18 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 and must take into account the possibility that calls to callbackfn may cause the this value to become detached.

This function is not generic. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the reduce method is 1.

22.2.3.20 %TypedArray%.prototype.reduceRight ( callbackfn [ , initialValue ] )

%TypedArray%.prototype.reduceRight is a distinct function that implements the same algorithm as Array.prototype.reduceRight as defined in 22.1.3.19 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 and must take into account the possibility that calls to callbackfn may cause the this value to become detached.

This function is not generic. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the reduceRight method is 1.

22.2.3.21 %TypedArray%.prototype.reverse ( )

%TypedArray%.prototype.reverse is a distinct function that implements the same algorithm as Array.prototype.reverse as defined in 22.1.3.20 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. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

22.2.3.22 %TypedArray%.prototype.set ( overloaded [ , offset ])

%TypedArray%.prototype.set is a single function whose behaviour is overloaded based upon the type of its first argument.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

The length property of the set method is 1.

22.2.3.22.1 %TypedArray%.prototype.set (array [ , offset ] )

Sets multiple values in this TypedArray, reading the values from the object array. The optional offset value indicates the first element index in this TypedArray where values are written. If omitted, it is assumed to be 0.

  1. Assert: array is any ECMAScript language value other than an Object with a [[TypedArrayName]] internal slot. If it is such an Object, the definition in 22.2.3.22.2 applies.
  2. Let target be the this value.
  3. If Type(target) is not Object, throw a TypeError exception.
  4. If target does not have a [[TypedArrayName]] internal slot, throw a TypeError exception.
  5. Assert: target has a [[ViewedArrayBuffer]] internal slot.
  6. Let targetOffset be ToInteger (offset).
  7. ReturnIfAbrupt(targetOffset).
  8. If targetOffset < 0, throw a RangeError exception.
  9. Let targetBuffer be the value of target's [[ViewedArrayBuffer]] internal slot.
  10. If IsDetachedBuffer(targetBuffer) is true, throw a TypeError exception.
  11. Let targetLength be the value of target's [[ArrayLength]] internal slot.
  12. Let targetName be the String value of target's [[TypedArrayName]] internal slot.
  13. Let targetElementSize be the Number value of the Element Size value specified in Table 49 for targetName.
  14. Let targetType be the String value of the Element Type value in Table 49 for targetName.
  15. Let targetByteOffset be the value of target's [[ByteOffset]] internal slot.
  16. Let src be ToObject(array).
  17. ReturnIfAbrupt(src).
  18. Let srcLength be ToLength(Get(src, "length")).
  19. ReturnIfAbrupt(srcLength).
  20. If srcLength + targetOffset > targetLength, throw a RangeError exception.
  21. Let targetByteIndex be targetOffset × targetElementSize + targetByteOffset.
  22. Let k be 0.
  23. Let limit be targetByteIndex + targetElementSize × srcLength.
  24. Repeat, while targetByteIndex < limit
    1. Let Pk be ToString(k).
    2. Let kNumber be ToNumber(Get(src, Pk)).
    3. ReturnIfAbrupt(kNumber).
    4. If IsDetachedBuffer(targetBuffer) is true, throw a TypeError exception.
    5. Perform SetValueInBuffer(targetBuffer, targetByteIndex, targetType, kNumber).
    6. Set k to k + 1.
    7. Set targetByteIndex to targetByteIndex + targetElementSize.
  25. Return undefined.

22.2.3.22.2 %TypedArray%.prototype.set(typedArray [, offset ] )

Sets multiple values in this TypedArray, reading the values from the typedArray argument object. The optional offset value indicates the first element index in this TypedArray where values are written. If omitted, it is assumed to be 0.

  1. Assert: typedArray has a [[TypedArrayName]] internal slot. If it does not, the definition in 22.2.3.22.1 applies.
  2. Let target be the this value.
  3. If Type(target) is not Object, throw a TypeError exception.
  4. If target does not have a [[TypedArrayName]] internal slot, throw a TypeError exception.
  5. Assert: target has a [[ViewedArrayBuffer]] internal slot.
  6. Let targetOffset be ToInteger (offset).
  7. ReturnIfAbrupt(targetOffset).
  8. If targetOffset < 0, throw a RangeError exception.
  9. Let targetBuffer be the value of target's [[ViewedArrayBuffer]] internal slot.
  10. If IsDetachedBuffer(targetBuffer) is true, throw a TypeError exception.
  11. Let targetLength be the value of target's [[ArrayLength]] internal slot.
  12. Let srcBuffer be the value of typedArray's [[ViewedArrayBuffer]] internal slot.
  13. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
  14. Let targetName be the String value of target's [[TypedArrayName]] internal slot.
  15. Let targetType be the String value of the Element Type value in Table 49 for targetName.
  16. Let targetElementSize be the Number value of the Element Size value specified in Table 49 for targetName.
  17. Let targetByteOffset be the value of target's [[ByteOffset]] internal slot.
  18. Let srcName be the String value of typedArray's [[TypedArrayName]] internal slot.
  19. Let srcType be the String value of the Element Type value in Table 49 for srcName .
  20. Let srcElementSize be the Number value of the Element Size value specified in Table 49 for srcName.
  21. Let srcLength be the value of typedArray's [[ArrayLength]] internal slot.
  22. Let srcByteOffset be the value of typedArray's [[ByteOffset]] internal slot.
  23. If srcLength + targetOffset > targetLength, throw a RangeError exception.
  24. If SameValue(srcBuffer, targetBuffer) is true, then
    1. Let srcBuffer be CloneArrayBuffer(targetBuffer, srcByteOffset, %ArrayBuffer%).
    2. NOTE: %ArrayBuffer% is used to clone targetBuffer because is it known to not have any observable side-effects.
    3. ReturnIfAbrupt(srcBuffer).
    4. Let srcByteIndex be 0.
  25. Else, let srcByteIndex be srcByteOffset.
  26. Let targetByteIndex be targetOffset × targetElementSize + targetByteOffset.
  27. Let limit be targetByteIndex + targetElementSize × srcLength.
  28. If SameValue(srcType, targetType) is false, then
    1. Repeat, while targetByteIndex < limit
      1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, srcType).
      2. Perform SetValueInBuffer (targetBuffer, targetByteIndex, targetType, value).
      3. Set srcByteIndex to srcByteIndex + srcElementSize.
      4. Set targetByteIndex to targetByteIndex + targetElementSize.
  29. Else,
    1. NOTE: If srcType and targetType are the same the transfer must be performed in a manner that preserves the bit-level encoding of the source data.
    2. Repeat, while targetByteIndex < limit
      1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, "Uint8").
      2. Perform SetValueInBuffer (targetBuffer, targetByteIndex, "Uint8", value).
      3. Set srcByteIndex to srcByteIndex + 1.
      4. Set targetByteIndex to targetByteIndex + 1.
  30. Return undefined.

22.2.3.23 %TypedArray%.prototype.slice ( start, end )

The interpretation and use of the arguments of %TypedArray%.prototype.slice are the same as for Array.prototype.slice as defined in 22.1.3.22. The following steps are taken:

  1. Let O be the this value.
  2. Let valid be ValidateTypedArray(O).
  3. ReturnIfAbrupt(valid).
  4. Let len be the value of O’s [[ArrayLength]] internal slot.
  5. Let relativeStart be ToInteger(start).
  6. ReturnIfAbrupt(relativeStart).
  7. If relativeStart < 0, let k be max((len + relativeStart),0); else let k be min(relativeStart, len).
  8. If end is undefined, let relativeEnd be len; else let relativeEnd be ToInteger(end).
  9. ReturnIfAbrupt(relativeEnd).
  10. If relativeEnd < 0, let final be max((len + relativeEnd),0); else let final be min(relativeEnd, len).
  11. Let count be max(finalk, 0).
  12. Let defaultConstructor be the intrinsic object listed in column one of Table 49 for the value of O's [[TypedArrayName]] internal slot.
  13. Let C be SpeciesConstructor(O, defaultConstructor).
  14. ReturnIfAbrupt(C).
  15. Let A be AllocateTypedArray(C, count).
  16. ReturnIfAbrupt(A).
  17. Let srcName be the String value of O’s [[TypedArrayName]] internal slot.
  18. Let srcType be the String value of the Element Type value in Table 49 for srcName.
  19. Let targetName be the String value of A’s [[TypedArrayName]] internal slot.
  20. Let targetType be the String value of the Element Type value in Table 49 for targetName.
  21. If SameValue(srcType, targetType) is false, then
    1. Let n be 0.
    2. Repeat, while k < final
      1. Let Pk be ToString(k).
      2. Let kValue be Get(O, Pk).
      3. ReturnIfAbrupt(kValue).
      4. Let status be Set(A, ToString(n), kValue, true ).
      5. ReturnIfAbrupt(status).
      6. Increase k by 1.
      7. Increase n by 1.
  22. Else if count > 0,
    1. Let srcBuffer be the value of O's [[ViewedArrayBuffer]] internal slot.
    2. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
    3. Let targetBuffer be the value of A's [[ViewedArrayBuffer]] internal slot.
    4. Let elementSize be the Number value of the Element Size value specified in Table 49 for srcType.
    5. NOTE: If srcType and targetType are the same the transfer must be performed in a manner that preserves the bit-level encoding of the source data.
    6. Let srcByteOffet be the value of O's [[ByteOffset]] internal slot.
    7. Let targetByteIndex be 0.
    8. Let srcByteIndex be (k × elementSize) + srcByteOffet.
    9. Repeat, while targetByteIndex < count × elementSize
      1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, "Uint8").
      2. Perform SetValueInBuffer (targetBuffer, targetByteIndex, "Uint8", value).
      3. Increase srcByteIndex by 1.
      4. Increase targetByteIndex by 1.
  23. Return A.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

The length property of the slice method is 2.

22.2.3.24 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] )

%TypedArray%.prototype.some is a distinct function that implements the same algorithm as Array.prototype.some as defined in 22.1.3.23 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 and must take into account the possibility that calls to callbackfn may cause the this value to become detached.

This function is not generic. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

The length property of the some method is 1.

22.2.3.25 %TypedArray%.prototype.sort ( comparefn )

%TypedArray%.prototype.sort is a distinct function that, except as described below, implements the same requirements as those of Array.prototype.sort as defined in 22.1.3.24. The implementation of the %TypedArray%.prototype.sort specification 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. The only internal methods of the this object that the algorithm may call are [[Get]] and [[Set]].

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

Upon entry, the following steps are performed to initialize evaluation of the sort function. These steps are used instead of the entry steps in 22.1.3.24:

  1. Let obj be the this value as the argument.
  2. Let buffer be ValidateTypedArray(obj).
  3. ReturnIfAbrupt(buffer).
  4. Let len be the value of obj's [[ArrayLength]] internal slot.

The implementation defined sort order condition for exotic objects is not applied by %TypedArray%.prototype.sort.

The following version of SortCompare is used by %TypedArray%.prototype.sort. It performs a numeric comparison rather than the string comparison used in 22.1.3.24. SortCompare has access to the comparefn and buffer values of the current invocation of the sort method.

When the TypedArray SortCompare abstract operation is called with two arguments x and y, the following steps are taken:

  1. Assert: Both Type(x) and Type(y) is Number.
  2. If the argument comparefn is not undefined, then
    1. Let v be Call(comparefn, undefined, «x, y»).
    2. ReturnIfAbrupt(v).
    3. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
    4. If v is NaN, return +0.
    5. Return v.
  3. If x and y are both NaN, return +0.
  4. If x is NaN, return 1.
  5. If y is NaN, return −1.
  6. If x < y, return −1.
  7. If x > y, return 1.
  8. If x is −0 and y is +0, return −1.
  9. If x is +0 and y is −0, return 1.
  10. Return +0.

NOTE Because NaN always compares greater than any other value, NaN property values always sort to the end of the result when comparefn is not provided.

22.2.3.26 %TypedArray%.prototype.subarray( [ begin [ , end ] ] )

Returns a new TypedArray object whose element type is the same as this TypedArray and whose ArrayBuffer is the same as the ArrayBuffer of this TypedArray, referencing the elements at begin, inclusive, up to end, exclusive. If either begin or end is negative, it refers to an index from the end of the array, as opposed to from the beginning.

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError exception.
  4. Assert: O has a [[ViewedArrayBuffer]] internal slot.
  5. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
  6. Let srcLength be the value of O's [[ArrayLength]] internal slot.
  7. Let relativeBegin be ToInteger(begin).
  8. ReturnIfAbrupt(relativeBegin).
  9. If relativeBegin < 0, let beginIndex be max((srcLength + relativeBegin), 0); else let beginIndex be min(relativeBegin, srcLength).
  10. If end is undefined, let relativeEnd be srcLength; else, let relativeEnd be ToInteger(end).
  11. ReturnIfAbrupt(relativeEnd).
  12. If relativeEnd < 0, let endIndex be max((srcLength + relativeEnd), 0); else let endIndex be min(relativeEnd, srcLength).
  13. Let newLength be max(endIndexbeginIndex, 0).
  14. Let constructorName be the String value of O's [[TypedArrayName]] internal slot.
  15. Let elementSize be the Number value of the Element Size value specified in Table 49 for constructorName.
  16. Let srcByteOffset be the value of O's [[ByteOffset]] internal slot.
  17. Let beginByteOffset be srcByteOffset + beginIndex × elementSize.
  18. Let defaultConstructor be the intrinsic object listed in column one of Table 49 for constructorName.
  19. Let constructor be SpeciesConstructor(O, defaultConstructor).
  20. ReturnIfAbrupt(constructor).
  21. Let argumentsList be «buffer, beginByteOffset, newLength».
  22. Return Construct(constructor, argumentsList).

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

The length property of the subarray method is 2.

22.2.3.27 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])

%TypedArray%.prototype.toLocaleString is a distinct function that implements the same algorithm as Array.prototype. toLocaleString as defined in 22.1.3.26 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. ValidateTypedArray is applied to the this value prior to evaluating the algorithm. If its result is an abrupt completion that exception is thrown instead of evaluating the algorithm.

NOTE If the ECMAScript implementation includes the ECMA-402 Internationalization API this function is based upon the algorithm for Array.prototype.toLocaleString that is in the ECMA-402 specification.

22.2.3.28 %TypedArray%.prototype.toString ( )

The initial value of the %TypedArray%.prototype.toString data property is the same built-in function object as the Array.prototype.toString method defined in 22.1.3.27.

22.2.3.29 %TypedArray%.prototype.values ( )

The following steps are taken:

  1. Let O be the this value.
  2. Let valid be ValidateTypedArray(O).
  3. ReturnIfAbrupt(valid).
  4. Return CreateArrayIterator(O, "value").

22.2.3.30 %TypedArray%.prototype [ @@iterator ] ( )

The initial value of the @@iterator property is the same function object as the initial value of the %TypedArray%.prototype.values property.

22.2.3.31 get %TypedArray%.prototype [ @@toStringTag ]

%TypedArray%.prototype[@@toStringTag] is an accessor property whose set accessor function is undefined. Its get accessor function performs the following steps:

  1. Let O be the this value.
  2. If Type(O) is not Object, return undefined.
  3. If O does not have a [[TypedArrayName]] internal slot, return undefined.
  4. Let name be the value of O's [[TypedArrayName]] internal slot.
  5. Assert: name is a String value.
  6. Return name.

This property has the attributes { [[Enumerable]]: false, [[Configurable]]: true }.

The initial value of the name property of this function is "get [Symbol.toStringTag]".

22.2.4 The TypedArray Constructors

Each of the TypedArray constructor objects is an intrinsic object that has the structure described below, differing only in the name used as the constructor name instead of TypedArray, in Table 49.

The TypedArray constructors are not intended to be called as a function and will throw an exception when called in that manner.

The TypedArray constructors are designed to be subclassable. They may be used as the value of an extends clause of a class definition. Subclass constructors that intend to inherit the specified TypedArray behaviour must include a super call to the TypedArray constructor to create and initialize the subclass instance with the internal state necessary to support the %TypedArray%.prototype built-in methods.

22.2.4.1 TypedArray( ... argumentsList)

A TypedArray constructor with a list of arguments argumentsList performs the following steps:

  1. If NewTarget is undefined, throw a TypeError exception.
  2. Let here be the active function.
  3. Let super be here.[[GetPrototypeOf]]().
  4. ReturnIfAbrupt(super).
  5. If IsConstructor (super) is false, throw a TypeError exception.
  6. Let argumentsList be the argumentsList argument of the [[Construct]] internal method that invoked the active function.
  7. Return Construct(super, argumentsList, NewTarget).

22.2.5 Properties of the TypedArray Constructors

The value of the [[Prototype]] internal slot of each TypedArray constructor is the %TypedArray% intrinsic object (22.2.1).

Each TypedArray constructor has a [[TypedArrayConstructorName]] internal slot property whose value is the String value of the constructor name specified for it in Table 49.

Each TypedArray constructor has a name property whose value is the String value of the constructor name specified for it in Table 49.

Besides a length property (whose value is 3), each TypedArray constructor has the following properties:

22.2.5.1 TypedArray.BYTES_PER_ELEMENT

The value of TypedArray.BYTES_PER_ELEMENT is the Number value of the Element Size value specified in Table 49 for TypedArray.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

22.2.5.2 TypedArray.prototype

The initial value of TypedArray.prototype is the corresponding TypedArray prototype intrinsic object (22.2.6).

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

22.2.6 Properties of TypedArray Prototype Objects

The value of the [[Prototype]] internal slot of a TypedArray prototype object is the intrinsic object %TypedArrayPrototype% (22.2.3). A TypedArray prototype object is an ordinary object. It does not have a [[ViewedArrayBuffer]] or any other of the internal slots that are specific to TypedArray instance objects.

22.2.6.1 TypedArray.prototype.BYTES_PER_ELEMENT

The value of TypedArray.prototype.BYTES_PER_ELEMENT is the Number value of the Element Size value specified in Table 49 for TypedArray.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

22.2.6.2 TypedArray.prototype.constructor

The initial value of a TypedArray.prototype.constructor is the corresponding %TypedArray% intrinsic object.

22.2.7 Properties of TypedArray Instances

TypedArray instances are Integer Indexed exotic objects. Each TypedArray instance inherits properties from the corresponding TypedArray prototype object. Each TypedArray instance has the following internal slots: [[TypedArrayName]], [[ViewedArrayBuffer]], [[ByteLength]], [[ByteOffset]], and [[ArrayLength]].