Skip to content

ArrayBuffer

The ArrayBuffer global object represents a block of raw binary memory, measured in bytes, that code can’t read or write directly. A view, such as a typed array like Uint8Array or a DataView, interprets those bytes as numbers of a given size and reads or writes them:

Language: JavaScript
> const buffer = new ArrayBuffer(4);
undefined
> buffer.byteLength
4
> buffer[0]
undefined

> const bytes = new Uint8Array(buffer);
undefined
> bytes[0] = 255;
255
> buffer
ArrayBuffer { [Uint8Contents]: <ff 00 00 00>, [byteLength]: 4 }

Every byte starts at zero. Indexing the buffer itself returns undefined because the bytes aren’t properties of the object. Several views can share one buffer, so a write through any of them changes the same memory.

Pick a view below, write a number into the four-byte buffer word, and watch the same bytes change as every view reads them back:

Interactive diagram — enable JavaScript to view.

Resizable buffers, created with the maxByteLength option, and .transfer() were added in ES2024. Resizable buffers are Baseline Newly available since 2024, and supported in Node.js 20 and later. The .transfer(), .transferToFixedLength(), and .detached members are Baseline Widely available.

Syntax

The ArrayBuffer constructor takes a size in bytes and an optional options object:

Language: JavaScript Syntax
new ArrayBuffer(<length>[, <options>])
Parameter Description
length The size of the buffer in bytes. A negative size, or one larger than maxByteLength, throws a RangeError
options An optional object. Its maxByteLength property makes the buffer resizable up to that many bytes

The constructor returns a new ArrayBuffer with every byte set to zero. The new keyword is required, and calling ArrayBuffer() without it throws a TypeError.

Key Static Methods and Properties

Member Kind Description
ArrayBuffer.isView() Static method Returns whether a value is a view over a buffer, such as a typed array or a DataView

Key Instance Methods and Properties

Member Kind Description
.byteLength Instance property Returns the size of the buffer in bytes, or 0 once it’s detached
.slice() Instance method Returns a new, fixed-length buffer holding a copy of a range of bytes
.resizable, .maxByteLength Instance property Return whether the buffer can be resized and the largest size it can reach (ES2024)
.resize() Instance method Grows or shrinks a resizable buffer in place, filling new bytes with zeros (ES2024)
.transfer(), .transferToFixedLength() Instance method Move the bytes into a new buffer and detach the original, keeping or dropping resizability (ES2024)
.detached Instance property Returns whether the buffer has been transferred and can no longer be used (ES2024)

Examples

A game server packs a message header with a two-byte protocol version followed by a four-byte payload length. A DataView writes both numbers into one buffer, and a Uint8Array over the same buffer shows the bytes that go over the wire:

Language: JavaScript
> const header = new ArrayBuffer(6);
undefined
> const view = new DataView(header);
undefined

> view.setUint16(0, 2);
undefined
> view.setUint32(2, 1024);
undefined
> new Uint8Array(header)
Uint8Array(6) [ 0, 2, 0, 0, 4, 0 ]

A sensor recorder doesn’t know in advance how many readings will arrive. A resizable buffer grows in place, and a typed array created without a length tracks the buffer’s current size:

Language: JavaScript
> const log = new ArrayBuffer(0, { maxByteLength: 1024 });
undefined
> const readings = new Uint8Array(log);
undefined
> readings.length
0

> log.resize(3);
undefined
> readings.set([10, 20, 30]);
undefined
> readings
Uint8Array(3) [ 10, 20, 30 ]

An image pipeline hands a finished frame to the next stage and must not keep writing to it. .transfer() moves the bytes into a new buffer and detaches the old one, which also empties every view over it:

Language: JavaScript
> const frame = new ArrayBuffer(4);
undefined
> const pixels = new Uint8Array(frame);
undefined
> pixels.fill(7);
Uint8Array(4) [ 7, 7, 7, 7 ]

> const sent = frame.transfer();
undefined
> frame.detached
true
> pixels.length
0
> new Uint8Array(sent)
Uint8Array(4) [ 7, 7, 7, 7 ]

Hosts can also transfer a buffer to another thread, for example through a worker’s postMessage(), which detaches the original in the same way.

Common Gotchas

Typed arrays read multibyte numbers in the platform’s byte order, which is little-endian on most platforms. A DataView defaults to big-endian, so the same four bytes give two different numbers:

Language: JavaScript
> const word = new ArrayBuffer(4);
undefined
> new DataView(word).setUint32(0, 1);
undefined

> new DataView(word).getUint32(0)
1
> new Uint32Array(word)[0]
16777216

The buffer’s size is in bytes, not elements. A typed array over a buffer whose size isn’t a multiple of its element size throws a RangeError:

Language: JavaScript
> new Float64Array(new ArrayBuffer(10))
Uncaught RangeError: byte length of Float64Array should be a multiple of 8
    at new Float64Array (<anonymous>)

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 12, 2026