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.
On this page
ArrayBuffer.prototype.slice()
Try it
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
start < 0,start + buffer.lengthis used. - If
start < -buffer.lengthorstartis omitted,0is used. - If
start >= buffer.length, nothing is extracted.
- Negative index counts back from the end of the buffer — if
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
end < 0,end + buffer.lengthis used. - If
end < -buffer.length,0is used. - If
end >= buffer.lengthorendis omitted,buffer.lengthis used, causing all elements until the end to be extracted. - If
endis positioned before or atstartafter normalization, nothing is extracted.
- 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
Browser compatibility
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
slice |
17 | 12 |
12The non-standardArrayBuffer.slice() method has been removed in Firefox 53 (but the standardized version ArrayBuffer.prototype.slice() is kept.
|
12.1 | 5.1 | 18 |
14The non-standardArrayBuffer.slice() method has been removed in Firefox 53 (but the standardized version ArrayBuffer.prototype.slice() is kept.
|
12.1 | 6 | 1.0 | 4.4 | 1.0 | 0.12.0 | |
See also
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice