TypedArray.prototype.slice()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

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().

Try it

Syntax

js
slice()
slice(start)
slice(start, end)

Parameters

start Optional

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

end Optional

Zero-based index at which to end extraction, converted to an integer. slice() extracts up to but not including end.

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

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

Specifications

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.slice

Browser compatibility

BCD tables only load in the browser

See also