Full answer
Normal arrays hold any values and are flexible. For binary data and large numeric data, JavaScript has a separate family.
ArrayBuffer: a block of bytes with a fixed length (new ArrayBuffer(16)), initialized to zeros. You cannot read it directly. You create a view.
Typed arrays: Uint8Array, Int16Array, Uint32Array, Float32Array, Float64Array, BigInt64Array, and others. Each interprets the buffer as a sequence of one element type. new Uint8Array(buffer) and new Uint32Array(buffer) can look at the same bytes in different ways, and a write through one is seen through the other. Values wrap on overflow: storing 256 in a Uint8Array gives 0. Uint8ClampedArray clamps to 0 to 255 instead, which suits canvas pixel data.
Offsets: a view can start at a byteOffset and cover a length. subarray(start, end) creates another view on the same memory, while slice(start, end) copies. This difference causes many bugs: changing a subarray changes the original.
DataView: reads and writes individual values at byte positions, with a choice of endianness: getUint16(offset, littleEndian). Typed arrays always use the platform byte order (little endian on almost all current devices), so use DataView for file formats and network protocols, which define their own byte order.
Other points: TextEncoder and TextDecoder convert between strings and Uint8Array as UTF-8. Buffers can be transferred between threads without copying (postMessage with a transfer list), and shared with SharedArrayBuffer. Resizable buffers (new ArrayBuffer(8, { maxByteLength: 64 }) and resize) exist in current engines. In Node.js, Buffer is a Uint8Array subclass with extra methods.
Why use them: less memory (a million numbers in a Float64Array use 8 MB, with no per element object overhead), predictable layout, fast access, and the ability to pass data to native and GPU APIs.
const buffer = new ArrayBuffer(8);
const bytes = new Uint8Array(buffer);
const words = new Uint16Array(buffer);
bytes[0] = 1;
bytes[1] = 2;
console.log('same memory, two views:', bytes.slice(0, 4), words[0], '(0x0201 on a little endian machine)');
const small = new Uint8Array([250, 255]);
small[0] += 10;
console.log('wraps on overflow:', small[0], new Uint8ClampedArray([300, -5, 1.5, 2.5]));
const original = new Uint8Array([1, 2, 3, 4]);
const view = original.subarray(1, 3);
const copy = original.slice(1, 3);
view[0] = 99;
console.log('subarray shares:', original, 'slice copies:', copy, 'view offset:', view.byteOffset);
const record = new DataView(new ArrayBuffer(8));
record.setUint16(0, 0x1234); // big endian by default
record.setUint16(2, 0x1234, true); // little endian
record.setFloat32(4, 1.5, true);
console.log('bytes:', [...new Uint8Array(record.buffer)].map((b) => b.toString(16).padStart(2, '0')).join(' '));
console.log('read back:', record.getUint16(0), record.getUint16(0, true), record.getFloat32(4, true));
const text = new TextEncoder().encode('h\u00e9llo');
console.log(text, new TextDecoder().decode(text), text.length, 'h\u00e9llo'.length);
const floats = new Float64Array(1000000);
console.log('Float64Array of a million numbers uses', floats.byteLength, 'bytes');
const resizable = new ArrayBuffer(4, { maxByteLength: 16 });
resizable.resize(12);
console.log('resizable:', resizable.byteLength, resizable.resizable, resizable.maxByteLength);
const transferred = buffer.transfer();
console.log('after transfer: old detached =', buffer.detached, 'old length', buffer.byteLength, 'new length', transferred.byteLength);
console.log(Buffer.from([1, 2, 3]) instanceof Uint8Array);Run with Node.js v24.22.1 as js-134.js; two runs gave identical output. Output: same memory, two views: Uint8Array(4) [ 1, 2, 0, 0 ] 513 (0x0201 on a little endian machine) wraps on overflow: 4 Uint8ClampedArray(4) [ 255, 0, 2, 2 ] subarray shares: Uint8Array(4) [ 1, 99, 3, 4 ] slice copies: Uint8Array(2) [ 2, 3 ] view offset: 1 bytes: 12 34 34 12 00 00 c0 3f read back: 4660 13330 1.5 Uint8Array(6) [ 104, 195, 169, 108, 108, 111 ] héllo 6 5 Float64Array of a million numbers uses 8000000 bytes resizable: 12 true 16 after transfer: old detached = true old length 0 new length 8 true
Key points
- ArrayBuffer is raw fixed-size memory; typed arrays are views that give it a numeric type.
- Several views can share one buffer; integer values wrap on overflow, and Uint8ClampedArray clamps.
- subarray shares memory; slice copies.
- DataView handles mixed types and explicit endianness for file and network formats.
- TextEncoder and TextDecoder convert strings; buffers can be transferred or shared between threads.
Common mistakes
- Assuming subarray copies, then changing the original by accident.
- Using typed arrays with the platform byte order for a file format that defines big endian.
- Expecting values to throw on overflow instead of wrapping.
- Using a normal array of numbers for very large numeric data.
Follow-ups
- How does transferring an ArrayBuffer to a Worker differ from sharing a SharedArrayBuffer?
- How would you parse a binary file header with DataView?
- Why is Uint8ClampedArray used for canvas pixel data?
Tips
- Separate the three ideas: buffer is memory, typed array is a typed view, DataView is a flexible reader.
- Say clearly that subarray shares and slice copies.
- Mention endianness for file and network formats.