ArrayBuffer

ArrayBuffer.prototype.slice

Returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's bytes from begin up to but not including end

Syntax

JavaScript
arrayBuffer.slice(begin, end?)

Parameters

ParameterTypeDescription
beginnumberZero-based byte index at which to begin
endnumberZero-based byte index before which to end

Return Value

A new ArrayBuffer containing the copied bytes

Examples

Basic Usage
const buffer = new ArrayBuffer(8)
new Uint8Array(buffer).set([1, 2, 3, 4, 5, 6, 7, 8])
const slice = buffer.slice(2, 6)
console.log(new Uint8Array(slice)) // [3, 4, 5, 6]
Practical Example
const buffer = new ArrayBuffer(16)
const firstHalf = buffer.slice(0, 8)
const secondHalf = buffer.slice(8)
console.log(firstHalf.byteLength, secondHalf.byteLength) // 8, 8
Advanced Usage
function splitBuffer(buf: ArrayBuffer, chunkSize: number): ArrayBuffer[] {
  const chunks: ArrayBuffer[] = []
  for (let i = 0; i < buf.byteLength; i += chunkSize) {
    chunks.push(buf.slice(i, i + chunkSize))
  }
  return chunks
}

Understanding ArrayBuffer.prototype.slice

The ArrayBuffer.prototype.slice method in JavaScript returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's bytes from begin up to but not including end. It belongs to the ArrayBuffer object and is one of the most widely used methods for working with arraybuffer values in modern JavaScript and TypeScript applications.

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

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

Browser support for ArrayBuffer.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 ArrayBuffer Methods

Other methods in the ArrayBuffer object

Related Tools

More ArrayBuffer Methods

Explore JavaScript Methods

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