Web/JavaScript/Reference/Global objects/TypedArray/subarray
The subarray() method returns a new TypedArray on the same ArrayBuffer store and with the same element types as for this TypedArray object. The begin offset is inclusive and the end offset is exclusive. TypedArray is one of the typed array types.
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.subarray([begin[, end]])
Parameters
beginOptional- Element to begin at. The offset is inclusive. The whole array will be included in the new view if this value is not specified.
endOptional- Element to end at. The offset is exclusive. If not specified, all elements from the one specified by
beginto the end of the array are included in the new view.
Return value
A new TypedArray object.
Description
The range specified by begin and end is clamped to the valid index range for the current array; if the computed length of the new array would be negative, it's clamped to zero. If either begin or end is negative, it refers to an index from the end of the array instead of from the beginning.
Also note that this is creating a new view on the existing buffer; changes to the new object's contents will impact the original object and vice versa.
Examples
Using the subarray() method
var buffer = new ArrayBuffer(8); var uint8 = new Uint8Array(buffer); uint8.set([1,2,3]); console.log(uint8); // Uint8Array [ 1, 2, 3, 0, 0, 0, 0, 0 ] var sub = uint8.subarray(0,4); console.log(sub); // Uint8Array [ 1, 2, 3, 0 ]
Specifications
| Specification |
|---|
| [(ECMA-262)The definition of 'TypedArray.prototype.subarray' 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 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
subarray
|
Chrome
Full support 7 |
Edge
Full support 14 |
Firefox
Full support 4 |
IE
Full support 10 |
Opera
Full support 11.6 |
Safari
Full support 5.1 |
WebView Android
Full support 4 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 12 |
Safari iOS
Full support 4.2 |
Samsung Internet Android
Full support 1.0 |
nodejs
Full support 0.12 |
Legend
- Full support
- Full support
See also
TypedArray.prototype.subarray() by Mozilla Contributors is licensed under CC-BY-SA 2.5.