JavaScript TypedArray subarray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

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.

Remember
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.

JavaScript
typedArray.subarray()
typedArray.subarray(start)
typedArray.subarray(start, end)
CallWhat 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
JavaScript
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

ParameterDescription
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 ValueDescription
TypedArrayA new view of the same type on the same buffer. It is a new object, but its elements are the original elements, not copies.

JavaScript Examples

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.

JavaScript
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
Try It Yourself

How It Works

  1. 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.
  2. Edits flow to the original. middle[0] = 99 writes into the shared memory, so pixels[1] is now 99.
  3. 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.

JavaScript
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
Try It Yourself

How It Works

  1. -2 means two from the end. subarray(-2) starts at index 4, and subarray(1, -1) stops just before the last element.
  2. Too far is just empty. Starting at index 10 is clamped to the length, so the view has length 0 and nothing throws.
  3. 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.

JavaScript
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
Try It Yourself

How It Works

  1. The view follows the original. After scores[1] = 100 the view prints 100, because it still points at the same bytes.
  2. The copy kept the old data. slice() made new memory when it was called, so the copy still prints 6 and 7.
  3. Different buffers, different fate. Changing the copy leaves scores alone, and only the view shares its buffer.

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

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.
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.
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.
Yes. subarray(-2) is the last two elements, and subarray(1, -1) skips the first and last element. Numbers beyond the ends are clamped.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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