Typed Arrays
Working with binary data in JavaScript — ArrayBuffer, Int8Array, Float32Array, DataView, and when to use typed arrays over regular arrays.
Integer typed arrays don't complain when a value doesn't fit. They wrap it around, keeping only the low bits, like a car odometer rolling over. In a Uint8Array, 250 + 60 doesn't become 310 or 255. It becomes 54, so a nearly white pixel turns nearly black. .map() has the same effect, because it returns the same type of typed array.
const pixels = new Uint8Array([100, 180, 250]); // three brightness values
for (let i = 0; i < pixels.length; i++) {
pixels[i] += 60; // brighten every pixel
}
console.log(pixels);
// Uint8Array(3) [ 160, 240, 54 ]When you need to work with raw binary data
Regular JavaScript arrays can hold anything — strings, objects, numbers, mixed types. Typed arrays are different: every element is the same fixed numeric type, stored in raw bytes. You use them when you need to work directly with binary data — reading an image file, sending data over a WebSocket, drawing with WebGL, or processing audio.
The two pieces: ArrayBuffer and a typed view
// ArrayBuffer — the raw bytes (you can't read them directly)
const buffer = new ArrayBuffer(8); // 8 bytes of memory
console.log(buffer.byteLength); // 8
// You need a "view" to read/write the bytes:
const view = new Uint8Array(buffer); // view as 8-bit unsigned integers
view[0] = 255;
view[1] = 127;
console.log(view[0], view[1]); // 255 127Available typed array types
Int8ArrayUint8ArrayUint8ClampedArrayInt16ArrayInt32ArrayFloat32ArrayFloat64ArrayBigInt64ArrayThat's what the "clamped" in Uint8ClampedArray in the table above is for. It still holds 0–255, but values outside that range are clamped to the nearest end instead of wrapping, which is what you want for colour and brightness. (It's also the type canvas pixel data uses.) For other integer types, check the range before you store a value, or use a wider type such as Int32Array or Float64Array for intermediate results:
const pixels = new Uint8ClampedArray([100, 180, 250]); // clamps to 0-255 instead of wrapping
for (let i = 0; i < pixels.length; i++) {
pixels[i] += 60;
}
console.log(pixels);
// Uint8ClampedArray(3) [ 160, 240, 255 ]Creating typed arrays
// From a length (zeroed memory):
const a = new Float32Array(4);
a[0] = 1.5; a[1] = 2.5;
console.log(a); // Float32Array [1.5, 2.5, 0, 0]
// From an array of values:
const b = new Int32Array([10, 20, 30, 40]);
console.log(b[2]); // 30
// From another typed array (copies values):
const c = new Float64Array(b);
console.log(c); // Float64Array [10, 20, 30, 40]
// Key properties:
console.log(b.length); // 4
console.log(b.byteLength); // 16 (4 × 4 bytes)
console.log(b.BYTES_PER_ELEMENT); // 4They support most array methods
const nums = new Float32Array([1, 2, 3, 4, 5]);
nums.map(x => x * 2); // Float32Array [2, 4, 6, 8, 10]
nums.filter(x => x > 3); // Float32Array [4, 5] ← wait, typed arrays have filter!
nums.reduce((a, b) => a + b); // 15
nums.forEach(x => console.log(x));
[...nums]; // [1, 2, 3, 4, 5] — spread works
// But: no .push(), .pop(), .splice() — fixed size!
// And: .map() returns a typed array of the same typeOfficial Sources
- MDN Web Docs — TypedArray
- MDN Web Docs — ArrayBuffer
- MDN Web Docs — DataView
- MDN Web Docs — SharedArrayBuffer
- ECMAScript 2024 — TypedArray Objects (§23.2)
The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.