Web/JavaScript/Reference/Global objects/TypedArray/slice
The slice() method returns a new typed array (with a new underlying buffer), that contains a copy of a portion of the original typed array. This method has the same algorithm as Array.prototype.slice(). TypedArray is one of the typed array types here.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
typedarray.slice([begin[, end]])
Parameters
beginOptional- Zero-based index at which to begin extraction.
- A negative index can be used, indicating an offset from the end of the sequence.
slice(-2)extracts the last two elements in the sequence. - If
beginis undefined,slicebegins from index0. endOptional- Zero-based index before which to end extraction.
sliceextracts up to but not includingend. - For example,
slice(1,4)extracts the second element through the fourth element (elements indexed 1, 2, and 3). - A negative index can be used, indicating an offset from the end of the sequence.
slice(2,-1)extracts the third element through the second-to-last element in the sequence. - If
endis omitted,sliceextracts through the end of the sequence (typedarray.length).
Return value
A new typed array containing the extracted elements.
Description
The slice method does not alter. It returns a shallow copy of elements from the original typed array.
If a new element is added to either typed array, the other typed array is not affected.
Examples
Return a portion of an existing typed array
const uint8 = new Uint8Array([1,2,3]);
uint8.slice(1); // Uint8Array [ 2, 3 ]
uint8.slice(2); // Uint8Array [ 3 ]
uint8.slice(-2); // Uint8Array [ 2, 3 ]
uint8.slice(0,1); // Uint8Array [ 1 ]
Polyfill
Since there is no global object with the name TypedArray, polyfilling must be done on an "as needed" basis.
if (!Uint8Array.prototype.slice) {
Object.defineProperty(Uint8Array.prototype, 'slice', {
value: function (begin, end)
{
return new Uint8Array(Array.prototype.slice.call(this, begin, end));
}
});
}
If you need to support truly obsolete JavaScript engines that don't support Object.defineProperty, it's best not to polyfill Array.prototype methods at all, as you can't make them non-enumerable.
Specifications
| Specification |
|---|
| [(ECMA-262)The definition of '%TypedArray%.prototype.slice' in that specification.] |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
slice
|
Chrome
Full support 45 |
Edge
Full support 14 |
Firefox
Full support 38 |
IE
No support No |
Opera
Full support 32 |
Safari
Full support 10 |
WebView Android
Full support 45 |
Chrome Android
Full support 45 |
Firefox Android
Full support 38 |
Opera Android
Full support 32 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 5.0 |
nodejs
Full support 4.0.0 |
Legend
- Full support
- Full support
- No support
- No support
See also
TypedArray.prototype.slice() by Mozilla Contributors is licensed under CC-BY-SA 2.5.