JavaScript ArrayBuffer

Beginner
8 min read
Updated: Nov 2026
3 examples
Binary data

Definition and Usage

In JavaScript, an ArrayBuffer is a fixed-length block of raw binary memory: a row of bytes, each holding a number from 0 to 255. You create one with new ArrayBuffer(byteLength), and every byte starts at 0.

You cannot read or write the bytes directly. buffer[0] is undefined. Instead you put a view on the buffer: a typed array such as Uint8Array or Float32Array, or a DataView. The view decides how the bytes are interpreted; the buffer only stores them.

You meet ArrayBuffers whenever JavaScript handles binary data: response.arrayBuffer() after fetch, file.arrayBuffer() for an uploaded file, WebSocket binary messages, images, audio, and custom file formats. This page teaches the ideas; the ArrayBuffer Methods index lists every method guide (slice(), transfer(), and more).

Buffer = memory, view = how you read it.

new Uint8Array(buffer)[0] reads the first byte. buffer.byteLength tells you how many bytes there are.

JavaScript Syntax

Common ways to create a buffer and look at its bytes:

JavaScript
const buffer = new ArrayBuffer(8);        // 8 bytes, all 0
const size = buffer.byteLength;            // 8
const bytes = new Uint8Array(buffer);      // view: 8 one-byte items
const view = new DataView(buffer);         // view: read/write any number type
const copy = buffer.slice(0, 4);           // new 4-byte buffer
const growable = new ArrayBuffer(4, { maxByteLength: 64 });  // ES2024

Core Ideas

Ideas every ArrayBuffer tutorial builds on:

IdeaExampleRemember
Createnew ArrayBuffer(16)16 bytes, all zero
Sizebuffer.byteLengthA property, not a method
Read / writenew Uint8Array(buffer)Always through a view
Copybuffer.slice(0, 4)New, independent buffer
Growbuffer.resize(32)Needs maxByteLength (ES2024)
Movebuffer.transfer()Original becomes detached (ES2024)
CheckArrayBuffer.isView(x)True for typed arrays and DataView

Typed Array Views Share Memory

A view never owns bytes; it points at a buffer. You can put several views on the same buffer, and a write through one is immediately visible through all the others. A view can also cover just part of a buffer: new Uint8Array(buffer, 4, 2) starts at byte 4 and is 2 bytes long (its byteOffset and byteLength).

Typed arrays read numbers in the computer's own byte order (little-endian on practically every device today). DataView lets you choose, which is why file formats and network protocols usually use it. To check whether a value is any kind of view, use ArrayBuffer.isView(). Need an independent copy instead of a shared view? Use slice().

Resizable Buffers

A normal ArrayBuffer keeps its size forever. Since ES2024 you can opt in to resizing by passing a limit: new ArrayBuffer(8, { maxByteLength: 64 }). The buffer starts at 8 bytes, buffer.resizable is true, and resize() changes its size in place, up to maxByteLength. New bytes are zero. A typed array created without a length follows the buffer as it grows and shrinks.

Transferring and Detaching

transfer() moves the bytes into a new ArrayBuffer and leaves the original detached: its byteLength becomes 0, buffer.detached is true, and methods such as slice() throw a TypeError. This is the same ownership hand-off that happens when you send a buffer to a Web Worker with worker.postMessage(buffer, [buffer]). transferToFixedLength() does the same but always returns a fixed-length buffer. Both are ES2024; check support before using them in older browsers.

ArrayBuffer vs SharedArrayBuffer

QuestionArrayBufferSharedArrayBuffer
Who can use it?One thread at a timeSeveral workers at the same time
Sending to a workerCopied, or moved with a transfer list (sender is detached)Shared: both sides see the same bytes
CoordinationNot neededUse Atomics to avoid race conditions
AvailabilityEverywhereBrowsers require a cross-origin isolated page

JavaScript Examples

Three short programs that show how an ArrayBuffer and its views work together.

Example 1 — A New Buffer Is All Zeros

Create a 4-byte buffer, look at it through a Uint8Array, and write two bytes.

JavaScript
const buffer = new ArrayBuffer(4);
const bytes = new Uint8Array(buffer);

console.log(buffer.byteLength);                  // 4
console.log(JSON.stringify(Array.from(bytes)));  // [0,0,0,0]

bytes[0] = 255;
bytes[1] = 256;
console.log(JSON.stringify(Array.from(bytes)));  // [255,0,0,0]
isView() tutorial

How It Works

  1. Memory starts zeroed. new ArrayBuffer(4) reserves 4 bytes, and every one of them starts as 0.
  2. Read through a view. The buffer has no indexes of its own; bytes is the Uint8Array window that reads and writes them.
  3. Bytes wrap at 256. A byte holds 0 to 255, so writing 256 wraps around to 0. Only the 255 is kept.

Example 2 — Two Views Share the Same Memory

A DataView writes 16-bit numbers; a Uint8Array over the same buffer sees the individual bytes change.

JavaScript
const buffer = new ArrayBuffer(4);
const bytes = new Uint8Array(buffer);
const view = new DataView(buffer);

view.setUint16(0, 258);        // big-endian by default
console.log(JSON.stringify(Array.from(bytes)));  // [1,2,0,0]

view.setUint16(2, 258, true);  // little-endian
console.log(JSON.stringify(Array.from(bytes)));  // [1,2,2,1]
console.log(view.getUint8(3));                   // 1
slice() tutorial

How It Works

  1. 258 is two bytes. 258 equals 1 times 256 plus 2, so it needs the bytes 1 and 2.
  2. Byte order is your choice. DataView writes big-endian unless you pass true, so the first write gives 1, 2 and the little-endian write gives 2, 1.
  3. No copies anywhere. Every write through view is instantly visible through bytes, because both views point at the same buffer.

Example 3 — Resize, Then Transfer (ES2024)

A quick tour of the two newer abilities: a buffer that can grow, and a transfer that detaches the original.

JavaScript
const buffer = new ArrayBuffer(2, { maxByteLength: 8 });
buffer.resize(6);
console.log(buffer.byteLength);  // 6

const moved = buffer.transfer();
console.log(buffer.detached);    // true
console.log(moved.byteLength);   // 6
console.log(moved.resizable);    // true

How It Works

  1. Opt in to resizing. The maxByteLength option allows resize(6) on a buffer that started with 2 bytes.
  2. Hand the bytes over. transfer() moves the 6 bytes into moved and marks buffer as detached.
  3. Settings travel along. The moved buffer is still resizable, with the same 8-byte limit as the original.

ArrayBuffer Method Groups

ArrayBuffer has only five methods. Each one has a full tutorial with examples you can run in the browser.

MethodUse it to
ArrayBuffer.isView()Is this value a typed array or DataView?
slice()Copy part of a buffer into a new buffer
resize()Grow or shrink a resizable buffer in place
transfer()Move bytes to a new buffer, keep resizability
transferToFixedLength()Move bytes to a new, fixed-length buffer

Full list with properties: ArrayBuffer Methods index.

Frequently Asked Questions

An ArrayBuffer is a block of raw binary memory with a fixed number of bytes. You create one with new ArrayBuffer(length) and read or write its bytes through a view such as Uint8Array or DataView.
The buffer only stores bytes; it does not know whether they are 8-bit integers, 32-bit floats, or text. A typed array or DataView decides how to interpret them, so buffer[0] is undefined while new Uint8Array(buffer)[0] is the first byte.
Many web APIs return them: response.arrayBuffer() from fetch, file.arrayBuffer() for a selected file, WebSocket and WebRTC binary messages, and the Web Audio and WebGL APIs. You can also build one yourself for a binary file format or network protocol.
Only if you create it as resizable: new ArrayBuffer(8, { maxByteLength: 64 }). Then resize() changes it in place. That and the transfer methods are ES2024 features, so check support in older browsers.
An ArrayBuffer belongs to one thread at a time and can be transferred, which detaches it. A SharedArrayBuffer can be used by several workers at once and needs Atomics for safe coordination; browsers only enable it on cross-origin isolated pages.

Browse every ArrayBuffer method

Open the index for isView(), resize(), slice(), transfer(), and transferToFixedLength().

ArrayBuffer Methods index →

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.

16 people found this page helpful