MDN self-hosted library · e01.org

TypedArray.prototype.slice()

The slice() method of TypedArray instances returns a copy of a portion of a typed array into a new typed array object selected from start to end (end not included) where start and end represent the index of items in that typed array. The original typed array will not be modified. This method has the same algorithm as Array.prototype.slice().

no runnable cells javascript-instance-method upstream source

The slice() method of TypedArray instances returns a copy of a portion of a typed array into a new typed array object selected from start to end (end not included) where start and end represent the index of items in that typed array. The original typed array will not be modified. This method has the same algorithm as Array.prototype.slice().

shorter

```js interactive-example const bytes = new Uint8Array([10, 20, 30, 40, 50]); const byteSlice = bytes.slice(1, 3);

console.log(byteSlice); // Expected output: Uint8Array [20, 30]


## Syntax

```js-nolint
slice()
slice(start)
slice(start, end)

Parameters

Return value

A new typed array containing the extracted elements.

Description

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

Examples

Return a portion of an existing typed array

const bytes = new Uint8Array([1, 2, 3]);
bytes.slice(1); // Uint8Array [ 2, 3 ]
bytes.slice(2); // Uint8Array [ 3 ]
bytes.slice(-2); // Uint8Array [ 2, 3 ]
bytes.slice(0, 1); // Uint8Array [ 1 ]

Specifications

Browser compatibility

See also