JavaScript TypedArray slice() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

typedArray.slice(start, end) makes a copy of a range and returns it as a new typed array of the same type. The copy has its own ArrayBuffer, so changing it never changes the original, and the other way round. The range starts at start and stops before end, and negative numbers count from the end. Use subarray() instead when you want a view that shares memory.

Remember
ta.slice()           → full copy
ta.slice(1, 4)       → copy of indexes 1, 2, 3
ta.slice(-2)         → copy of the last two
ta.slice(3, 1)       → empty copy

copy has its own buffer (independent)
subarray() = view (shared), slice() = copy

Syntax

Both arguments are optional. The end index is not included.

JavaScript
typedArray.slice()
typedArray.slice(start)
typedArray.slice(start, end)
CallWhat it does
ta.slice(1, 4)Copy the elements at indexes 1, 2, and 3.
ta.slice(-2)Copy the last two elements.
ta.slice()Copy the whole typed array into new memory.

The type is kept: slicing an Int16Array returns an Int16Array. Available since ES2015 on all 11 types. Not to be confused with Array.prototype.slice(), which returns a normal Array.

Library declarations
JavaScript
TypedArray.prototype.slice(start = 0, end = length)

// returns a new typed array of the SAME type
// the copy has its own buffer: copy.buffer !== original.buffer
// copy.byteOffset is always 0

Parameters

ParameterDescription
start (optional)Index of the first element to copy. 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 typed array of the same type with a new buffer. If the range is empty, its length is 0.

JavaScript Examples

Example 1 — Copy a Range, Then Change It

Temperatures are stored in an Int16Array. We copy the middle of the series and edit the copy only.

JavaScript
const temps = new Int16Array([12, -4, 8, 15, 21]);
const middle = temps.slice(1, 4);

console.log(middle.join(' '));    // -4 8 15

middle[0] = 100;
console.log(middle.join(' '));    // 100 8 15
console.log(temps.join(' '));     // 12 -4 8 15 21

console.log(middle.buffer === temps.buffer);   // false
console.log(middle instanceof Int16Array);     // true
Try It Yourself

How It Works

  1. The end index is skipped. slice(1, 4) takes indexes 1, 2, and 3, which are -4, 8, and 15.
  2. Edits stay in the copy. Setting middle[0] = 100 does not touch temps, which still prints its original five numbers.
  3. New memory, same type. The two buffers are different objects, and the copy is still an Int16Array.

Example 2 — start, end, and Negative Indexes

Both arguments are optional and may be negative, which counts from the end. A start that is past the end gives an empty copy.

JavaScript
const bytes = new Uint8Array([10, 20, 30, 40, 50, 60]);

console.log(bytes.slice(2).join(' '));       // 30 40 50 60
console.log(bytes.slice(-2).join(' '));      // 50 60
console.log(bytes.slice(1, -1).join(' '));   // 20 30 40 50
console.log(bytes.slice(-3, -1).join(' '));  // 40 50
console.log(bytes.slice(4, 2).length);       // 0
console.log(bytes.slice().length);           // 6
Try It Yourself

How It Works

  1. Only a start. slice(2) copies from index 2 to the very end, which gives four elements.
  2. Negative means from the end. The numbers -2, -1, and -3 are turned into positions 4, 5, and 3 by adding the length 6.
  3. Backwards or empty is fine. A start after the end copies nothing, and no argument at all copies all six elements.

Example 3 — Take a Snapshot of Changing Data

A copy is a frozen snapshot. A Float32Array of readings is copied before an update, and a slice of a view gets its own buffer.

JavaScript
const live = new Float32Array([0.5, 1.5, 2.5, 3.5]);
const snapshot = live.slice();

live.fill(0);
console.log(live.join(' '));       // 0 0 0 0
console.log(snapshot.join(' '));   // 0.5 1.5 2.5 3.5

const big = new Uint8Array([1, 2, 3, 4, 5, 6]);
const part = big.subarray(2, 4).slice();
console.log(part.byteOffset);                // 0
console.log(part.buffer.byteLength);         // 2
console.log(big.subarray(2, 4).byteOffset);  // 2
Try It Yourself

How It Works

  1. Copy first, change later. The snapshot is taken before fill(0) wipes the live array, so it keeps 0.5 to 3.5.
  2. A slice of a view is compact. The view starts at byte 2 in a six-byte buffer, but its slice has a new two-byte buffer.
  3. The offsets prove it. The copy begins at byteOffset 0, while the view still starts at 2 inside the original buffer.

Browser & Runtime Support

TypedArray.prototype.slice() is part of ES2015 (ES6), together with the other array-like typed array methods.

✓ Baseline · ES2015

TypedArray.prototype.slice()

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 11 have typed arrays, but not this method
Not supported
slice() Excellent

Bottom line: Safe everywhere modern JavaScript runs. Internet Explorer 10 and 11 have typed arrays, but not slice(); they only offer subarray().

Frequently Asked Questions

It copies a range of elements into a new typed array of the same type. The original is not changed, and the copy has its own buffer.
slice() copies the data and the result is independent. subarray() returns a view of the same memory, so changes show up in both arrays.
Yes. slice(-2) copies the last two elements, and slice(1, -1) leaves out the first and last. They count back from the length.
No. It only reads. Unlike a view from subarray(), editing the slice never changes the source.
No. TypedArray slice() returns a typed array of the same type. Use Array.from(ta.slice()) if you want a regular Array.

Next: some()

Test whether at least one element passes a condition, and stop as soon as one does.

some() 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