ArrayBuffer.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 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.
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.
- Negative index counts back from the end of the buffer — if
-buffer.length <= start < 0
,start + buffer.length
is used. - If
start < -buffer.length
orstart
is omitted,0
is used. - If
start >= buffer.length
, an empty buffer is returned.
- Negative index counts back from the end of the buffer — if
end
Optional-
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. - If
end < -buffer.length
,0
is used. - If
end >= buffer.length
orend
is omitted,buffer.length
is used, causing all elements until the end to be extracted. - If
end
implies a position before or at the position thatstart
implies, an empty buffer is returned.
- Negative index counts back from the end of the buffer — if
Return value
A new ArrayBuffer
containing the extracted elements.
Examples
Copying an ArrayBuffer
js
const buf1 = new ArrayBuffer(8);
const buf2 = buf1.slice(0);
Specifications
Specification |
---|
ECMAScript Language Specification # sec-arraybuffer.prototype.slice |
Browser compatibility
BCD tables only load in the browser