docs.rodeo

MDN Web Docs mirror

ArrayBuffer.prototype.slice()

{{JSRef}} 

The slice() method of {{jsxref("ArrayBuffer")}}  instances returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer’s bytes from start, inclusive, up to end, exclusive. If either start or end is negative, it refers to an index from the end of the array, as opposed to from the beginning.

{{InteractiveExample("JavaScript Demo: ArrayBuffer.slice()")}} 

// Create an ArrayBuffer with a size in bytes
const buffer = new ArrayBuffer(16);
const int32View = new Int32Array(buffer);
// Produces Int32Array [0, 0, 0, 0]

int32View[1] = 42;
const sliced = new Int32Array(buffer.slice(4, 12));
// Produces Int32Array [42, 0]

console.log(sliced[0]);
// Expected output: 42

Syntax

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

Parameters

Return value

A new {{jsxref("ArrayBuffer")}}  containing the extracted elements. It is not resizable, even if the original was.

Examples

Copying an ArrayBuffer

const buf1 = new ArrayBuffer(8);
const buf2 = buf1.slice(0);

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN