TypedArray

TypedArray.prototype.slice

Returns a shallow copy of a portion of a typed array into a new typed array, with a new underlying ArrayBuffer

Syntax

JavaScript
typedArray.slice(begin?, end?)

Parameters

ParameterTypeDescription
beginnumberIndex to start extraction
endnumberIndex before which to end extraction

Return Value

A new TypedArray containing the extracted elements

Examples

Basic Usage
const arr = new Uint8Array([1, 2, 3, 4, 5])
const copy = arr.slice(1, 4)
console.log(copy) // Uint8Array [2, 3, 4]
Practical Example
const arr = new Float64Array([1.1, 2.2, 3.3])
const last = arr.slice(-1)
console.log(last) // Float64Array [3.3]
Advanced Usage
const original = new Int32Array([10, 20, 30])
const copy = original.slice()
copy[0] = 99
console.log(original[0]) // 10 (independent copy)

Understanding TypedArray.prototype.slice

The TypedArray.prototype.slice method in JavaScript returns a shallow copy of a portion of a typed array into a new typed array, with a new underlying ArrayBuffer. It belongs to the TypedArray object and is one of the most widely used methods for working with typedarray values in modern JavaScript and TypeScript applications.

The method signature is typedArray.slice(begin?, end?). It accepts 2 parameters: begin, end. When called, it returns a new typedarray containing the extracted elements. Understanding when and how to use slice() helps you write more expressive, readable code.

Common use cases for TypedArray.prototype.slice include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like typedarray-subarray, typedarray-set, arraybuffer-slice, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for TypedArray.prototype.slice is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More TypedArray Methods

Other methods in the TypedArray object

Related Tools

More TypedArray Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.