Typed Arrays and Binary Data

Pixels, audio samples, GPU vertex data and network frames need fixed-size raw bytes. An ArrayBuffer is a plain block of bytes; a view gives them a number type, offset and length. Views come in 1-byte (Int8Array, Uint8Array, Uint8ClampedArray), 2-byte (Int16Array, Uint16Array, Float16Array), 4-byte (Int32Array, Uint32Array, Float32Array) and 8-byte (Float64Array, BigInt64Array, BigUint64Array) flavors, and a DataView reads mixed types.

One 8-byte ArrayBuffer read through four different views
One 8-byte ArrayBuffer read through four different views

Multi-byte views use the CPU's byte order, little-endian on virtually every current device. Typed arrays share most Array methods but have a fixed length, so push(), pop(), splice() and concat() are missing; they add set() to copy values in and subarray(), a view that shares memory (whereas slice() copies).

Shared memory and conversion on writeJavaScriptLive
const bytes = Int8Array.from([1, 2, 3]);
bytes.subarray(1)[0] = 99;             // writes through a second view
console.log(bytes, bytes.BYTES_PER_ELEMENT, Array.isArray(bytes));
console.log(new Uint8Array([256, -1, 1.7]), new Uint8ClampedArray([256, -1, 1.7]));

Integer views wrap out-of-range values modulo 2^n and truncate fractions; Uint8ClampedArray, used by canvas ImageData, clamps to 0-255 and rounds.

Subsections