Home JavaScript TypedArray Methods slice() JavaScript TypedArray slice() Method 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.
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.
typedArray.slice()
typedArray.slice(start)
typedArray.slice(start, end) Call What 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 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 Parameter Description 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 Value Description TypedArray A new typed array of the same type with a new buffer. If the range is empty, its length is 0.
Important
slice() copies the elements , so it costs time and memory for big ranges. In return, the result does not depend on the source, so you can edit either one freely. If you only need to read part of the data and want to avoid the copy, use subarray(). Also, a slice of a view starts a fresh buffer that holds only the sliced bytes.
JavaScript Examples 1. An independent copy 2. Negative indexes 3. Snapshot and fresh buffer 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.
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 -4 8 15
100 8 15
12 -4 8 15 21
false
true How It Works The end index is skipped. slice(1, 4) takes indexes 1, 2, and 3, which are -4, 8, and 15.Edits stay in the copy. Setting middle[0] = 100 does not touch temps, which still prints its original five numbers.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.
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 30 40 50 60
50 60
20 30 40 50
40 50
0
6 How It Works Only a start. slice(2) copies from index 2 to the very end, which gives four elements.Negative means from the end. The numbers -2, -1, and -3 are turned into positions 4, 5, and 3 by adding the length 6.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.
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 0 0 0 0
0.5 1.5 2.5 3.5
0
2
2 How It Works Copy first, change later. The snapshot is taken before fill(0) wipes the live array, so it keeps 0.5 to 3.5.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.The offsets prove it. The copy begins at byteOffset 0, while the view still starts at 2 inside the original buffer.Compatibility
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 What does TypedArray slice() do? 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.
What is the difference between slice() and subarray()? slice() copies the data and the result is independent. subarray() returns a view of the same memory, so changes show up in both arrays.
Can slice() use negative indexes? Yes. slice(-2) copies the last two elements, and slice(1, -1) leaves out the first and last. They count back from the length.
Does slice() change the original typed array? No. It only reads. Unlike a view from subarray(), editing the slice never changes the source.
Does slice() return a normal Array? 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 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