Typed Arrays
A typed array is a fixed-length view over a block of raw bytes, where every element is the same numeric width. It is how JavaScript reads and writes binary data such as image pixels, network frames, and audio samples.
Detailed Explanation
Ordinary arrays hold arbitrary values and can have gaps. That flexibility is useless when you are handed 4 megabytes of pixel data and need to treat it as unsigned bytes. Typed arrays exist for exactly that case, and they were introduced for WebGL before becoming general-purpose.
The design splits into two parts, and keeping them straight is the whole concept.
An ArrayBuffer is the storage: a fixed number of raw bytes with no interpretation. You cannot read or write it directly. A view gives those bytes a type. new Uint8Array(buffer) says "read this as unsigned 8-bit integers". new Float64Array(buffer) says "read the same bytes as 64-bit floats". Several views can share one buffer, and writing through one is immediately visible through the others, because there is only one copy of the bytes.
The view kinds are Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, BigInt64Array, and BigUint64Array. A view has length in elements and byteLength in bytes, plus buffer and byteOffset telling you which slice of storage it covers.
Three behaviours differ sharply from normal arrays. Typed arrays are fixed length — no push, no pop, and length is read-only. The one exception is the ES2024 resizable ArrayBuffer, created with new ArrayBuffer(n, { maxByteLength }). They cannot hold holes; every slot has a value, and an unwritten slot is 0, not undefined. And they coerce on write instead of storing what you gave them. Assigning 300 to a Uint8Array slot stores 44, because the value wraps modulo 256. Uint8ClampedArray clamps to 255 instead, which is what canvas pixel data wants. Out-of-range indices are also silent: writing arr[999] on a 4-element typed array does nothing and throws nothing.
Endianness is where people get burned. Typed array elements wider than one byte use the platform's native byte order, which is little-endian on essentially every machine you will meet. Network protocols and most file formats are big-endian. DataView is the fix: its getUint32/setUint32-style methods take an explicit littleEndian boolean, defaulting to big-endian. Use DataView for parsing formats, and typed arrays for bulk numeric work.
In Node.js, Buffer is a subclass of Uint8Array, so buf instanceof Uint8Array is true and any API accepting a typed array accepts a Buffer. In browsers, fetch(...).then(r => r.arrayBuffer()) is the usual entry point.
Costs are pleasant and predictable. Element access is O(1) with no hashing and no boxing, memory is exactly length × bytesPerElement with no per-element object overhead, and iteration is cache-friendly. slice copies in O(n); subarray returns a zero-copy view of the same buffer, which is the one to use in hot loops. An ArrayBuffer can also be transferred to a worker, moving ownership without copying, and SharedArrayBuffer lets two threads see the same memory.
Code Examples
One buffer, two views: the bytes are shared, the interpretation is not.
// One buffer, two views: the bytes are shared, the interpretation is not.
const buffer = new ArrayBuffer(8); // 8 raw bytes, all zero
const asBytes = new Uint8Array(buffer);
const asFloat = new Float64Array(buffer); // 8 bytes = exactly one double
asFloat[0] = 1.5;
// 1.5 as an IEEE 754 double is 0x3FF8000000000000, shown here low byte first
console.log(asBytes); // => Uint8Array(8) [ 0, 0, 0, 0, 0, 0, 248, 63 ]
console.log(asBytes.byteLength, asFloat.length); // => 8 1
// Writes coerce instead of storing the value you gave.
const pixels = new Uint8Array(3);
pixels[0] = 300;
console.log(pixels[0]); // => 44 wraps modulo 256
const canvasPixels = new Uint8ClampedArray(3);
canvasPixels[0] = 300;
console.log(canvasPixels[0]); // => 255 clamped, what image data needs
// Out-of-bounds writes are silently dropped, not appended.
pixels[99] = 1;
console.log(pixels.length, pixels[99]); // => 3 undefinedDataView when byte order is part of the format you are parsing.
// DataView when byte order is part of the format you are parsing.
const frame = new ArrayBuffer(4);
const view = new DataView(frame);
view.setUint32(0, 0x12345678); // default is big-endian (network order)
console.log([...new Uint8Array(frame)]); // => [ 18, 52, 86, 120 ]
view.setUint32(0, 0x12345678, true); // true = little-endian
console.log([...new Uint8Array(frame)]); // => [ 120, 86, 52, 18 ]
// subarray shares memory; slice copies it.
const samples = new Int16Array([1, 2, 3, 4]);
const windowView = samples.subarray(1, 3); // no allocation of sample data
windowView[0] = 99;
console.log(samples[1]); // => 99 the parent changed too
const copy = samples.slice(1, 3);
copy[0] = -1;
console.log(samples[1]); // => 99 the parent is untouchedNode.js only: Buffer is a Uint8Array subclass.
// Node.js only: Buffer is a Uint8Array subclass.
const bytes = Buffer.from("héllo", "utf8");
console.log(bytes instanceof Uint8Array); // => true
console.log(bytes.length); // => 6 "é" takes two bytes in UTF-8
// A Buffer is a view into a pooled ArrayBuffer, so it usually has an offset.
console.log(bytes.byteOffset >= 0, bytes.buffer.byteLength >= 6); // => true trueCommon Gotchas
Writes wrap silently
storing `256` in a `Uint8Array` gives `0` and storing `-1` gives `255`. There is no error and no warning, so validate values before writing them.
Endianness is platform-dependent
multi-byte typed array elements use native byte order. Parsing a big-endian file format with `Uint32Array` produces byte-swapped garbage on x86 and ARM. Use `DataView`.
Views must fit their buffer
`new Uint32Array(buffer, 1)` throws a `RangeError` because the byte offset must be a multiple of the element size, and the requested length must fit inside the buffer.
Fixed length, no `push`
growing means allocating a larger buffer and copying with `set`. Size the buffer up front, or use a resizable `ArrayBuffer`.