recursal/MDN huggingface dataset:
Uint16Array() constructor
=========================

The **`Uint16Array()`** constructor creates `Uint16Array` objects. The contents are initialized to `0`.

Syntax
------


```js
new Uint16Array()
new Uint16Array(length)
new Uint16Array(typedArray)
new Uint16Array(object)

new Uint16Array(buffer)
new Uint16Array(buffer, byteOffset)
new Uint16Array(buffer, byteOffset, length)
```


**Note:** `Uint16Array()` can only be constructed with `new`. Attempting to call it without `new` throws a `TypeError`.

### Parameters

See `TypedArray`.

### Exceptions

See `TypedArray`.

Examples
--------

### Different ways to create a Uint16Array


```js
// From a length
const uint16 = new Uint16Array(2);
uint16[0] = 42;
console.log(uint16[0]); // 42
console.log(uint16.length); // 2
console.log(uint16.BYTES\_PER\_ELEMENT); // 2

// From an array
const x = new Uint16Array([21, 31]);
console.log(x[1]); // 31

// From another TypedArray
const y = new Uint16Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
const buffer = new ArrayBuffer(16);
const z = new Uint16Array(buffer, 2, 4);
console.log(z.byteOffset); // 2

// From an iterable
const iterable = (function\* () {
  yield\* [1, 2, 3];
})();
const uint16FromIterable = new Uint16Array(iterable);
console.log(uint16FromIterable);
// Uint16Array [1, 2, 3]
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-typedarray-constructors |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `Uint16Array` in `core-js`
* JavaScript typed arrays guide
* `TypedArray`
* `ArrayBuffer`
* `DataView`
TypedArray.prototype.subarray()
===============================

 The **`subarray()`** method of `TypedArray` instances returns a new typed array
 on the same `ArrayBuffer` store and with the same element types as for this
 typed array. The begin offset is **inclusive** and the end
 offset is **exclusive**.

Try it
------

Syntax
------


```js
subarray()
subarray(begin)
subarray(begin, end)
```

### Parameters

`begin` Optional

 Element to begin at. The offset is inclusive. The whole array will be included in
 the new view if this value is not specified.


`end` Optional

 Element to end at. The offset is exclusive. If not specified, all elements from the
 one specified by `begin` to the end of the array are included in
 the new view.


### Return value

A new `TypedArray` object.

Description
-----------

 The range specified by `begin` and `end` is
 clamped to the valid index range for the current array; if the computed length of the
 new array would be negative, it's clamped to zero. If either
 `begin` or `end` is negative, it refers to
 an index from the end of the array instead of from the beginning.

 Also note that this is creating a new view on the existing buffer; changes to the new
 object's contents will impact the original object and vice versa.

Examples
--------

### Using the subarray() method


```js
const buffer = new ArrayBuffer(8);
const uint8 = new Uint8Array(buffer);
uint8.set([1, 2, 3]);

console.log(uint8); // Uint8Array [ 1, 2, 3, 0, 0, 0, 0, 0 ]

const sub = uint8.subarray(0, 4);

console.log(sub); // Uint8Array [ 1, 2, 3, 0 ]
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-%typedarray%.prototype.subarray |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `TypedArray.prototype.subarray` in `core-js`
* JavaScript typed arrays guide
* `TypedArray`
* `ArrayBuffer`
TypedArray.prototype.every()
============================

The **`every()`** method of `TypedArray` instances tests whether all elements in the typed array pass the test implemented by the provided function. It returns a Boolean value. This method has the same algorithm as `Array.prototype.every()`.

Try it
------

Syntax
------


```js
every(callbackFn)
every(callbackFn, thisArg)
```

### Parameters

`callbackFn`

A function to execute for each element in the typed array. It should return a truthy value to indicate the element passes the test, and a falsy value otherwise. The function is called with the following arguments:

`element`

The current element being processed in the typed array.

`index`

The index of the current element being processed in the typed array.

`array`

The typed array `every()` was called upon.


`thisArg` Optional

A value to use as `this` when executing `callbackFn`. See iterative methods.

### Return value

`true` unless `callbackFn` returns a falsy value for a typed array element, in which case `false` is immediately returned.

Description
-----------

See `Array.prototype.every()` for more details. This method is not generic and can only be called on typed array instances.

Examples
--------

### Testing size of all typed array elements

The following example tests whether all elements in the typed array are 10 or bigger.


```js
function isBigEnough(element, index, array) {
  return element >= 10;
}
new Uint8Array([12, 5, 8, 130, 44]).every(isBigEnough); // false
new Uint8Array([12, 54, 18, 130, 44]).every(isBigEnough); // true
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-%typedarray%.prototype.every |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `TypedArray.prototype.every` in `core-js`
* JavaScript typed arrays guide
* `TypedArray`
* `TypedArray.prototype.forEach()`
* `TypedArray.prototype.some()`
* `TypedArray.prototype.find()`
* `Array.prototype.every()`
TypedArray.prototype.at()
=========================

The **`at()`** method of `TypedArray` instances takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the typed array. This method has the same algorithm as `Array.prototype.at()`.

Try it
------

Syntax
------


```js
at(index)
```

### Parameters

`index`

Zero-based index of the typed array element to be returned, converted to an integer. Negative index counts back from the end of the typed array — if `index < 0`, `index + array.length` is accessed.

### Return value

The element in the typed array matching the given index. Always returns `undefined` if `index < -array.length` or `index >= array.length` without attempting to access the corresponding property.

Description
-----------

See `Array.prototype.at()` for more details. This method is not generic and can only be called on typed array instances.

Examples
--------

### Return the last value of a typed array

The following example provides a function which returns the last element found in a specified array.


```js
const uint8 = new Uint8Array([1, 2, 4, 7, 11, 18]);

// A function which returns the last item of a given array
function returnLast(arr) {
  return arr.at(-1);
}

const lastItem = returnLast(uint8);
console.log(lastItem); // 18
```

### Comparing methods

Here we compare different ways to select the penultimate (last but one) item of a `TypedArray`. Whilst all below methods are valid, it highlights the succinctness and readability of the `at()` method.


```js
// Our typed array with values
const uint8 = new Uint8Array([1, 2, 4, 7, 11, 18]);

// Using length property
const lengthWay = uint8[uint8.length - 2];
console.log(lengthWay); // 11

// Using slice() method. Note an array is returned
const sliceWay = uint8.slice(-2, -1);
console.log(sliceWay[0]); // 11

// Using at() method
const atWay = uint8.at(-2);
console.log(atWay); // 11
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-%typedarray%.prototype.at |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `TypedArray.prototype.at` in `core-js`
* JavaScript typed arrays guide
* `TypedArray`
* `TypedArray.prototype.findIndex()`
* `TypedArray.prototype.indexOf()`
* `TypedArray.prototype.with()`
* `Array.prototype.at()`
* `String.prototype.at()`
TypedArray.prototype.includes()
===============================

The **`includes()`** method of `TypedArray` instances determines whether a typed array includes a certain value among its entries, returning `true` or `false` as appropriate. This method has the same algorithm as `Array.prototype.includes()`.

Try it
------

Syntax
------


```js
includes(searchElement)
includes(searchElement, fromIndex)
```

### Parameters

`searchElement`

The value to search for.

`fromIndex` Optional

Zero-based index at which to start searching, converted to an integer.

### Return value

A boolean value which is `true` if the value `searchElement` is found within the typed array (or the part of the typed array indicated by the index `fromIndex`, if specified).

Description
-----------

See `Array.prototype.includes()` for more details. This method is not generic and can only be called on typed array instances.

Examples
--------

### Using includes()


```js
const uint8 = new Uint8Array([1, 2, 3]);
uint8.includes(2); // true
uint8.includes(4); // false
uint8.includes(3, 3); // false

// NaN handling (only true for Float32 and Float64)
new Uint8Array([NaN]).includes(NaN); // false, since the NaN passed to the constructor gets converted to 0
new Float32Array([NaN]).includes(NaN); // true;
new Float64Array([NaN]).includes(NaN); // true;
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-%typedarray%.prototype.includes |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `TypedArray.prototype.includes` in `core-js`
* JavaScript typed arrays guide
* `TypedArray`
* `TypedArray.prototype.indexOf()`
* `TypedArray.prototype.find()`
* `TypedArray.prototype.findIndex()`
* `Array.prototype.includes()`
* `String.prototype.includes()`
TypedArray.prototype.reduceRight()
==================================

The **`reduceRight()`** method of `TypedArray` instances applies a function against an accumulator and each value of the typed array (from right-to-left) to reduce it to a single value. This method has the same algorithm as `Array.prototype.reduceRight()`.

Try it
------

Syntax
------


```js
reduceRight(callbackFn)
reduceRight(callbackFn, initialValue)
```

### Parameters

`callbackFn`

A function to execute for each element in the typed array. Its return value becomes the value of the `accumulator` parameter on the next invocation of `callbackFn`. For the last invocation, the return value becomes the return value of `reduceRight()`. The function is called with the following arguments:

`accumulator`

The value resulting from the previous call to `callbackFn`. On the first call, its value is `initialValue` if the latter is specified; otherwise its value is the last element of the typed array.

`currentValue`

The value of the current element. On the first call, its value is the last element if `initialValue` is specified; otherwise its value is the second-to-last element.

`currentIndex`

The index position of `currentValue` in the typed array. On the first call, its value is `array.length - 1` if `initialValue` is specified, otherwise `array.length - 2`.

`array`

The typed array `reduceRight()` was called upon.


`initialValue` Optional

Value to use as accumulator to the first call of the `callbackFn`. If no initial value is supplied, the last element in the typed array will be used and skipped. Calling `reduceRight()` on an empty typed array without an initial value creates a `TypeError`.

### Return value

The value that results from the reduction.

Description
-----------

See `Array.prototype.reduceRight()` for more details. This method is not generic and can only be called on typed array instances.

Examples
--------

### Sum up all values within an array


```js
const total = new Uint8Array([0, 1, 2, 3]).reduceRight((a, b) => a + b);
// total === 6
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-%typedarray%.prototype.reduceright |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `TypedArray.prototype.reduceRight` in `core-js`
* JavaScript typed arrays guide
* `TypedArray`
* `TypedArray.prototype.map()`
* `TypedArray.prototype.reduce()`
* `Array.prototype.reduceRight()`
* `Object.groupBy()`
* `Map.groupBy()`
TypedArray.prototype.entries()
==============================

The **`entries()`** method of `TypedArray` instances returns a new *array iterator* object that contains the key/value pairs for each index in the typed array. This method has the same algorithm as `Array.prototype.entries()`.

Try it
------

Syntax
------


```js
entries()
```

### Parameters

None.

### Return value

A new iterable iterator object.

Description
-----------

See `Array.prototype.entries()` for more details. This method is not generic and can only be called on typed array instances.

Examples
--------

### Iteration using for...of loop


```js
const array = new Uint8Array([10, 20, 30, 40, 50]);
const arrayEntries = arr.entries();
for (const element of arrayEntries) {
  console.log(element);
}
```

### Alternative iteration


```js
const array = new Uint8Array([10, 20, 30, 40, 50]);
const arrayEntries = arr.entries();

console.log(arrayEntries.next().value); // [0, 10]
console.log(arrayEntries.next().value); // [1, 20]
console.log(arrayEntries.next().value); // [2, 30]
console.log(arrayEntries.next().value); // [3, 40]
console.log(arrayEntries.next().value); // [4, 50]
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-%typedarray%.prototype.entries |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `TypedArray.prototype.entries` in `core-js`
* JavaScript typed arrays guide
* `TypedArray`
* `TypedArray.prototype.keys()`
* `TypedArray.prototype.values()`
* `TypedArray.prototype[@@iterator]()`
* `Array.prototype.entries()`
* Iteration protocols
TypedArray.prototype[@@iterator]()
==================================

The **`[@@iterator]()`** method of `TypedArray` instances implements the iterable protocol and allows typed arrays to be consumed by most syntaxes expecting iterables, such as the spread syntax and `for...of` loops. It returns an array iterator object that yields the value of each index in the typed array.

The initial value of this property is the same function object as the initial value of the `TypedArray.prototype.values` property.

Try it
------

Syntax
------


```js
typedArray[Symbol.iterator]()
```

### Parameters

None.

### Return value

The same return value as `TypedArray.prototype.values()`: a new iterable iterator object that yields the value of each index in the typed array.

Examples
--------

### Iteration using for...of loop

Note that you seldom need to call this method directly. The existence of the `@@iterator` method makes typed arrays iterable, and iterating syntaxes like the `for...of` loop automatically call this method to obtain the iterator to loop over.


```js
const arr = new Uint8Array([10, 20, 30, 40, 50]);
for (const n of arr) {
  console.log(n);
}
```

### Manually hand-rolling the iterator

You may still manually call the `next()` method of the returned iterator object to achieve maximum control over the iteration process.


```js
const arr = new Uint8Array([10, 20, 30, 40, 50]);
const arrIter = arr[Symbol.iterator]();
console.log(arrIter.next().value); // 10
console.log(arrIter.next().value); // 20
console.log(arrIter.next().value); // 30
console.log(arrIter.next().value); // 40
console.log(arrIter.next().value); // 50
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-%typedarray%.prototype-@@iterator |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `TypedArray.prototype[@@iterator]` in `core-js`
* JavaScript typed arrays guide
* `TypedArray`
* `TypedArray.prototype.entries()`
* `TypedArray.prototype.keys()`
* `TypedArray.prototype.values()`
* `Symbol.iterator`
* Iteration protocols
TypedArray.prototype.findLastIndex()
====================================

The **`findLastIndex()`** method of `TypedArray` instances iterates the typed array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned. This method has the same algorithm as `Array.prototype.findLastIndex()`.

Try it
------

Syntax
------


```js
findLastIndex(callbackFn)
findLastIndex(callbackFn, thisArg)
```

### Parameters

`callbackFn`

A function to execute for each element in the typed array. It should return a truthy value to indicate a matching element has been found, and a falsy value otherwise. The function is called with the following arguments:

`element`

The current element being processed in the typed array.

`index`

The index of the current element being processed in the typed array.

`array`

The typed array `findLastIndex()` was called upon.


`thisArg` Optional

A value to use as `this` when executing `callbackFn`. See iterative methods.

### Return value

 The index of the last (highest-index) element in the typed array that passes the test.
 Otherwise `-1` if no matching element is found.

Description
-----------

See `Array.prototype.findLastIndex()` for more details. This method is not generic and can only be called on typed array instances.

Examples
--------

### Find the index of the last prime number in a typed array

The following example returns the index of the last element in the typed array that is a prime number, or `-1` if there is no prime number.


```js
function isPrime(element) {
  if (element % 2 === 0 || element < 2) {
    return false;
  }
  for (let factor = 3; factor <= Math.sqrt(element); factor += 2) {
    if (element % factor === 0) {
      return false;
    }
  }
  return true;
}

let uint8 = new Uint8Array([4, 6, 8, 12]);
console.log(uint8.findLastIndex(isPrime));
// -1 (no primes in array)
uint8 = new Uint8Array([4, 5, 7, 8, 9, 11, 12]);
console.log(uint8.findLastIndex(isPrime));
// 5
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-%typedarray%.prototype.findlastindex |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `TypedArray.prototype.findLastIndex` in `core-js`
* JavaScript typed arrays guide
* `TypedArray`
* `TypedArray.prototype.find()`
* `TypedArray.prototype.findIndex()`
* `TypedArray.prototype.findLast()`
* `TypedArray.prototype.indexOf()`
* `TypedArray.prototype.lastIndexOf()`
* `Array.prototype.findLastIndex()`
TypedArray.prototype.toSorted()
===============================

The **`toSorted()`** method of `TypedArray` instances is the copying version of the `sort()` method. It returns a new typed array with the elements sorted in ascending order. This method has the same algorithm as `Array.prototype.toSorted()`, except that it sorts the values numerically instead of as strings by default.

Syntax
------


```js
toSorted()
toSorted(compareFn)
```

### Parameters

`compareFn` Optional

Specifies a function that defines the sort order. If omitted, the typed array elements are sorted according to numeric value.

`a`

The first element for comparison.

`b`

The second element for comparison.


### Return value

A new typed array with the elements sorted in ascending order.

Description
-----------

See `Array.prototype.toSorted()` for more details. This method is not generic and can only be called on typed array instances.

Examples
--------

### Sorting an array

For more examples, see also the `Array.prototype.sort()` method.


```js
const numbers = new Uint8Array([40, 1, 5, 200]);
const numberSorted = numbers.toSorted();
console.log(numberSorted); // Uint8Array [ 1, 5, 40, 200 ]
// Unlike plain Arrays, a compare function is not required
// to sort the numbers numerically.
console.log(numbers); // Uint8Array [ 40, 1, 5, 200 ]
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-%typedarray%.prototype.tosorted |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `TypedArray.prototype.toSorted` in `core-js`
* JavaScript typed arrays guide
* `TypedArray.prototype.sort()`
* `TypedArray.prototype.toReversed()`
* `TypedArray.prototype.with()`
* `Array.prototype.toSorted()`
TypedArray.prototype.sort()
===========================

The **`sort()`** method of `TypedArray` instances sorts the elements of a typed array *in place* and returns the reference to the same typed array, now sorted. This method has the same algorithm as `Array.prototype.sort()`, except that it sorts the values numerically instead of as strings by default.

Try it
------

Syntax
------


```js
sort()
sort(compareFn)
```

### Parameters

`compareFn` Optional

A function that defines the sort order. The return value should be a number whose sign indicates the relative order of the two elements: negative if `a` is less than `b`, positive if `a` is greater than `b`, and zero if they are equal. `NaN` is treated as `0`. The function is called with the following arguments:

`a`

The first element for comparison. Will never be `undefined`.

`b`

The second element for comparison. Will never be `undefined`.

If omitted, the typed array elements are sorted according to numeric value.

### Return value

The reference to the original typed array, now sorted. Note that the typed array is sorted *in place*, and no copy is made.

Description
-----------

See `Array.prototype.sort()` for more details. This method is not generic and can only be called on typed array instances.

Examples
--------

### Using sort()

For more examples, see also the `Array.prototype.sort()` method.


```js
let numbers = new Uint8Array([40, 1, 5, 200]);
numbers.sort();
// Uint8Array [ 1, 5, 40, 200 ]
// Unlike plain Arrays, a compare function is not required
// to sort the numbers numerically.

// Regular Arrays require a compare function to sort numerically:
numbers = [40, 1, 5, 200];
numbers.sort();
// [1, 200, 40, 5]

numbers.sort((a, b) => a - b); // compare numbers
// [ 1, 5, 40, 200 ]
```

Specifications
--------------

| Specification |
| --- |
| ECMAScript Language Specification # sec-%typedarray%.prototype.sort |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Polyfill of `TypedArray.prototype.sort` with modern behavior like stable sort in `core-js`
* JavaScript typed arrays guide
* `TypedArray`
* `TypedArray.prototype.reverse()`
* `TypedArray.prototype.toSorted()`
* `Array.prototype.sort()`