Photo by chansu shin on Unsplash

On this page

This chapter follows Map and WeakMap.

JavaScript arrays are flexible object lists. ArrayBuffer is fixed-length raw memory; typed array constructors create views that interpret those bytes as Int8, Uint32, Float64, and so on.

ArrayBuffer

const buffer = new ArrayBuffer(16); // 16 bytes, zero-filled
buffer.byteLength; // 16

A buffer is not directly indexed. You read and write through a view.

Typed array views

Constructor Bytes per element Signed
Int8Array, Uint8Array 1 int / unsigned
Int16Array, Uint16Array 2
Int32Array, Uint32Array 4
Float32Array, Float64Array 4 / 8 IEEE floats
Uint8ClampedArray 1 Clamps to 0–255 (canvas)
BigInt64Array, BigUint64Array 8 64-bit integers
const buf = new ArrayBuffer(12);
const floats = new Float32Array(buf); // 3 float32 slots
floats[0] = 1.5;
floats[1] = 2.5;
floats.length; // 3

Multiple views can overlap the same buffer at different offsets:

const ab = new ArrayBuffer(8);
const u32 = new Uint32Array(ab);
const u8 = new Uint8Array(ab, 4, 4); // second half only

DataView

DataView reads and writes mixed widths and endianness at arbitrary byte offsets—useful for file formats and network packets.

const dv = new DataView(new ArrayBuffer(8));
dv.setUint16(0, 0x1234, true); // little-endian
dv.getUint16(0, true); // 0x1234

Compared to Array

Array Typed array
Element type Any Fixed numeric (or bigint)
Length Dynamic Fixed by buffer × type
Methods Full array prototype Subset (map, subarray, …)
JSON Native Often converted manually

subarray(begin, end) returns another view on the same buffer (copy-on-write semantics differ from slice on arrays—still a view, not a deep copy).

Transfer and resizing (ES2024)

ArrayBuffer.prototype.transfer(newByteLength?) can move bytes to a new buffer and detach the old one—relevant for workers and zero-copy handoff. Check engine support before relying on it in production.

Binary data in APIs (fetch arrayBuffer(), Web Crypto, Web Audio) usually surfaces as ArrayBuffer or typed arrays—not as [number] lists.

This is the last chapter in Collections. For built-in JSON, dates, and regex, see JSON in JavaScript.

Search articles

Type a word to search published articles.

Open the search page