title | slug | page-type | browser-compat |
---|---|---|---|
SharedArrayBuffer.prototype.slice() |
Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/slice |
javascript-instance-method |
javascript.builtins.SharedArrayBuffer.slice |
{{JSRef}}
The slice()
method of {{jsxref("SharedArrayBuffer")}} instances returns a new SharedArrayBuffer
whose contents are a copy of this SharedArrayBuffer
'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.
{{EmbedInteractiveExample("pages/js/sharedarraybuffer-slice.html")}}
slice()
slice(start)
slice(start, end)
start
{{optional_inline}}- : 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
, nothing is extracted.
- Negative index counts back from the end of the buffer — if
- : Zero-based index at which to start extraction, converted to an integer.
end
{{optional_inline}}- : 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, nothing is extracted.
- Negative index counts back from the end of the buffer — if
- : Zero-based index at which to end extraction, converted to an integer.
A new {{jsxref("SharedArrayBuffer")}} containing the extracted elements.
const sab = new SharedArrayBuffer(1024);
sab.slice(); // SharedArrayBuffer { byteLength: 1024 }
sab.slice(2); // SharedArrayBuffer { byteLength: 1022 }
sab.slice(-2); // SharedArrayBuffer { byteLength: 2 }
sab.slice(0, 1); // SharedArrayBuffer { byteLength: 1 }
{{Specifications}}
{{Compat}}
- {{jsxref("SharedArrayBuffer")}}
- {{jsxref("ArrayBuffer.prototype.slice()")}}