JavaScript ArrayBuffer slice() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayBuffer

Definition

buffer.slice(begin, end) returns a new ArrayBuffer holding a copy of the bytes from begin up to, but not including, end. The original buffer is not changed. Negative positions count back from the end, just like Array slice(). Because the result is a copy, writing to it never affects the original. That is the key difference from a typed array view or subarray(), which share memory.

Remember
buffer.slice(begin, end)   → new ArrayBuffer (a copy)

slice(0, 4)   → bytes 0, 1, 2, 3
slice(-2)     → the last 2 bytes
slice()       → copy of the whole buffer
slice(4, 1)   → empty buffer (byteLength 0)

copy, not a view: edits do not flow back

Syntax

Call slice on an ArrayBuffer with zero, one, or two positions.

JavaScript
buffer.slice(begin)
buffer.slice(begin, end)
CallWhat it does
buffer.slice()Copy the whole buffer.
buffer.slice(begin)Copy from begin to the end.
buffer.slice(begin, end)Copy from begin up to, not including, end.

Built into JavaScript since ES2015. The copy is always a fixed-length buffer, even when you slice a resizable one.

Library declarations
JavaScript
buffer.slice(begin, end)   // returns a new ArrayBuffer

// begin: default 0; negative counts from the end
// end:   default buffer.byteLength; negative counts from the end
// both are clamped to the range 0 .. byteLength

Parameters

ParameterDescription
beginOptional. Index of the first byte to copy (default 0). A negative value counts from the end: -2 means two bytes before the end.
endOptional. Index to stop before (default byteLength). Negative values count from the end, and values past the end are clamped.

Return Value

Return ValueDescription
ArrayBufferA brand-new, fixed-length ArrayBuffer with the copied bytes. Its byteLength is end - begin, or 0 when end is not after begin.

JavaScript Examples

Example 1 — Copy the Header of a File

Reading a file with file.arrayBuffer() or fetch(url).then((r) => r.arrayBuffer()) gives you one big ArrayBuffer. Here the first bytes of a PNG image are typed in by hand so the example runs anywhere; slice() copies the 4-byte signature out.

JavaScript
const fileBytes = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13]);
const fileBuffer = fileBytes.buffer;

const header = fileBuffer.slice(0, 4);
const headerBytes = new Uint8Array(header);

console.log(header.byteLength);                        // 4
console.log(JSON.stringify(Array.from(headerBytes)));  // [137,80,78,71]
console.log(String.fromCharCode(headerBytes[1], headerBytes[2], headerBytes[3]));  // PNG
console.log(fileBuffer.byteLength);                    // 12
Try It Yourself

How It Works

  1. Start from raw file bytes. Every PNG file begins with byte 137 followed by the letters P, N, and G. fileBytes.buffer is the underlying 12-byte ArrayBuffer.
  2. Copy the first four bytes. slice(0, 4) copies indexes 0 to 3 into a new 4-byte buffer; index 4 is the stop position and is not included.
  3. The source keeps its size. Decoding bytes 80, 78, 71 as character codes gives PNG, and fileBuffer still holds all 12 bytes.

Example 2 — A Copy Is Independent, a View Is Not

Take the same three bytes twice: once with slice() and once with a typed array view. Then write to both.

JavaScript
const buffer = new Uint8Array([1, 2, 3, 4, 5, 6]).buffer;

const copy = new Uint8Array(buffer.slice(2, 5));
const view = new Uint8Array(buffer, 2, 3);

copy[0] = 99;
view[1] = 77;

console.log(JSON.stringify(Array.from(copy)));                    // [99,4,5]
console.log(JSON.stringify(Array.from(view)));                    // [3,77,5]
console.log(JSON.stringify(Array.from(new Uint8Array(buffer))));  // [1,2,3,77,5,6]
Try It Yourself

How It Works

  1. Same bytes, two ways. Both copy and view start out as 3, 4, 5. One is a fresh buffer; the other is a window into the original.
  2. Edit each one. Writing 99 into the copy stays in the copy. Writing 77 through the view changes index 3 of the original buffer.
  3. Pick by intent. Use slice() for an independent snapshot. Use a view, or subarray() on a typed array, when changes should be shared and nothing should be copied.

Example 3 — Negative Indexes and Edge Cases

slice() never throws because of its positions. It counts negative values from the end and clamps everything else.

JavaScript
const buffer = new Uint8Array([10, 20, 30, 40, 50, 60]).buffer;
const show = (buf) => JSON.stringify(Array.from(new Uint8Array(buf)));

console.log(show(buffer.slice(-2)));         // [50,60]
console.log(show(buffer.slice(1, -3)));      // [20,30]
console.log(show(buffer.slice(2, 100)));     // [30,40,50,60]
console.log(buffer.slice(4, 1).byteLength);  // 0
console.log(buffer.slice() === buffer);      // false
Try It Yourself

How It Works

  1. Negative begin. slice(-2) starts two bytes before the end, at index 4, and copies the last two bytes.
  2. Mixed and clamped ends. An end of -3 becomes index 3, so slice(1, -3) copies indexes 1 and 2. An end of 100 is clamped to 6.
  3. Empty and full copies. When end is not after begin you get a 0-byte buffer, not an error. slice() copies everything into a different object.

Browser & Runtime Support

ArrayBuffer.prototype.slice() is part of ES2015 (ES6), together with typed arrays and DataView.

✓ Baseline · ES2015

ArrayBuffer.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 Retired · not a modern target
Legacy
slice() Excellent

Bottom line: Safe in every modern browser and runtime. The detached-buffer TypeError only matters in code that also uses the newer ES2024 transfer() methods or transfers buffers to workers.

Frequently Asked Questions

A new ArrayBuffer containing a copy of the selected bytes. The original buffer keeps all of its bytes and its size.
slice() copies bytes into a new buffer, so edits do not affect the original. subarray() creates another view over the same memory, so edits are shared and nothing is copied.
Yes. Negative values count from the end, so buffer.slice(-4) copies the last four bytes. Out-of-range values are clamped instead of throwing.
No. Even if you slice a resizable buffer, the copy is a fixed-length ArrayBuffer whose resizable property is false. Use transfer() if the new buffer must stay resizable.
Only when the buffer is detached, for example after buffer.transfer() or after it was sent to a worker in a postMessage transfer list. Then slice() throws a TypeError. Positions never cause errors.

Next: transfer()

Move a buffer's bytes into a new buffer and detach the old one.

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