Home JavaScript TypedArray Methods subarray() JavaScript TypedArray subarray() Method Definition typedArray.subarray(start, end) returns a new typed array view of the same ArrayBuffer. It does not copy anything: the original and the view share the same memory , so a change through one is visible in the other. The range starts at start and stops before end, and negative numbers count from the end. Use slice() instead when you want a real copy.
ta.subarray() → view of everything
ta.subarray(1, 4) → view of indexes 1, 2, 3
ta.subarray(-2) → view of the last two
view and original share memory (no copy)
view[0] = 99 → ta[1] is 99 too
slice() = copy, subarray() = view Syntax Both arguments are optional. The end index is not included.
typedArray.subarray()
typedArray.subarray(start)
typedArray.subarray(start, end) Call What it does ta.subarray(1, 4)A view of the elements at indexes 1, 2, and 3. ta.subarray(-2)A view of the last two elements. ta.subarray(2).byteOffsetWhere the view starts inside the shared buffer, in bytes.
Part of the original typed array API and in ES2015 on all 11 types. There is no subarray() on regular Arrays, because they cannot share memory this way.
Library declarations TypedArray.prototype.subarray(start = 0, end = length)
// returns a new typed array of the SAME type and the SAME buffer
// view.buffer === original.buffer
// view.byteOffset = original.byteOffset + start * BYTES_PER_ELEMENT Parameters Parameter Description start (optional) Index of the first element in the view. Negative counts from the end. Defaults to 0. end (optional) Index to stop before. Negative counts from the end. Defaults to the length.
Return Value Return Value Description TypedArray A new view of the same type on the same buffer . It is a new object, but its elements are the original elements, not copies.
Important
A view is cheap, but it keeps the whole buffer alive and writes through to the original. If you need data that no longer depends on the source, call slice(). Out-of-range numbers are clamped, so ta.subarray(10) on a short array gives an empty view instead of throwing.
JavaScript Examples 1. A shared view 2. Negative indexes and byteOffset 3. subarray() vs slice() Example 1 — Change Data Through a View A row of five bytes holds pixel brightness. We take a view of the middle three and edit it, then look at the original.
const pixels = new Uint8Array([10, 20, 30, 40, 50]);
const middle = pixels.subarray(1, 4);
console.log(middle.join(' ')); // 20 30 40
console.log(middle.length); // 3
middle[0] = 99;
console.log(pixels.join(' ')); // 10 99 30 40 50
pixels[3] = 7;
console.log(middle.join(' ')); // 99 30 7 20 30 40
3
10 99 30 40 50
99 30 7 How It Works Indexes 1 to 3 are in the view. The end index 4 is not included, so the view holds 20, 30, and 40 and has length 3.Edits flow to the original. middle[0] = 99 writes into the shared memory, so pixels[1] is now 99.And back again. Changing pixels[3] shows up as the last element of middle, because both objects read the same bytes.Example 2 — Negative Indexes, byteOffset, and buffer Negative numbers count from the end. An Int16Array view also shows where it begins in the shared buffer.
const bytes = new Uint8Array([1, 2, 3, 4, 5, 6]);
console.log(bytes.subarray(-2).join(' ')); // 5 6
console.log(bytes.subarray(1, -1).join(' ')); // 2 3 4 5
console.log(bytes.subarray(10).length); // 0
const shorts = new Int16Array([100, 200, 300, 400]);
const tail = shorts.subarray(2);
console.log(tail.join(' ')); // 300 400
console.log(tail.byteOffset); // 4
console.log(tail.buffer === shorts.buffer); // true 5 6
2 3 4 5
0
300 400
4
true How It Works -2 means two from the end. subarray(-2) starts at index 4, and subarray(1, -1) stops just before the last element.Too far is just empty. Starting at index 10 is clamped to the length, so the view has length 0 and nothing throws.The offset is in bytes. Index 2 of an Int16Array is 2 x 2 = 4 bytes in, and buffer proves both objects use one ArrayBuffer.Example 3 — View Versus Copy We take the same range twice, once as a view and once as a copy, then change the original.
const scores = new Uint8Array([5, 6, 7, 8]);
const view = scores.subarray(1, 3);
const copy = scores.slice(1, 3);
scores[1] = 100;
console.log(view.join(' ')); // 100 7
console.log(copy.join(' ')); // 6 7
copy[1] = 0;
console.log(scores.join(' ')); // 5 100 7 8
console.log(view.buffer === scores.buffer); // true
console.log(copy.buffer === scores.buffer); // false 100 7
6 7
5 100 7 8
true
false How It Works The view follows the original. After scores[1] = 100 the view prints 100, because it still points at the same bytes.The copy kept the old data. slice() made new memory when it was called, so the copy still prints 6 and 7.Different buffers, different fate. Changing the copy leaves scores alone, and only the view shares its buffer.Compatibility
Browser & Runtime Support TypedArray.prototype.subarray() was in the first typed array API and is part of ES2015 (ES6).
✓ Baseline · ES2015
TypedArray.prototype.subarray() Supported in every modern browser, plus Node.js, Deno, and Bun.
98% Modern browser support
Google Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer IE 10 and newer
Full support subarray() Excellent
Bottom line: Safe everywhere. Even Internet Explorer 10 and 11 had subarray(), because it came with the very first typed arrays.
Frequently Asked Questions What does TypedArray subarray() return? A new typed array of the same type that is a view of the same buffer. It covers the range from start up to, but not including, end.
Does subarray() copy the data? No. The view and the original share the same memory. Changing an element through one changes it in the other, and no bytes are duplicated.
What is the difference between subarray() and slice()? subarray() gives a view that shares memory. slice() gives a copy with its own buffer. Use slice() when later changes must not affect the other array.
Do negative indexes work? Yes. subarray(-2) is the last two elements, and subarray(1, -1) skips the first and last element. Numbers beyond the ends are clamped.
How do I find where the view starts in the buffer? Read view.byteOffset. It is counted in bytes, so for an Int16Array view starting at index 2 it is 4.
Next: toLocaleString() Turn the elements into a locale-formatted string such as 1,234.5 for a chosen language.
toLocaleString() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion