Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

ArrayBuffer.prototype.slice()

BaselineWidely available

Theslice() method ofArrayBuffer instances returns a newArrayBuffer whose contents are a copy of thisArrayBuffer's bytes fromstart, inclusive, up toend, exclusive. If eitherstart orend is negative, it refers to an index from the end of the array, as opposed to from the beginning.

Try it

// Create an ArrayBuffer with a size in bytesconst 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

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

Parameters

startOptional

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

  • Negative index counts back from the end of the buffer — if-buffer.length <= start < 0,start + buffer.length is used.
  • Ifstart < -buffer.length orstart is omitted,0 is used.
  • Ifstart >= buffer.length, an empty buffer is returned.
endOptional

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

  • Negative index counts back from the end of the buffer — if-buffer.length <= end < 0,end + buffer.length is used.
  • Ifend < -buffer.length,0 is used.
  • Ifend >= buffer.length orend is omitted isundefined,buffer.length is used, causing all elements until the end to be extracted.
  • Ifend implies a position before or at the position thatstart implies, an empty buffer is returned.

Return value

A newArrayBuffer containing the extracted elements. It is notresizable, even if the original was.

Examples

Copying an ArrayBuffer

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

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-arraybuffer.prototype.slice

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp