← thecodex.expert · The Codex Family of Knowledge
The Codex / JavaScript / Typed Arrays

Typed Arrays

Working with binary data in JavaScript — ArrayBuffer, Int8Array, Float32Array, DataView, and when to use typed arrays over regular arrays.

What it is, in plain English: A typed array is an array-like view over a raw binary buffer (ArrayBuffer) where every element has the same fixed type and byte width — Int8, Uint8, Int16, Float32, Float64, etc. Unlike regular JavaScript arrays (which can hold any mix of values), typed arrays map directly to memory, making them essential for WebGL, audio processing, binary file formats, network protocols, and any performance-critical numerical work.
Where this bites you

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  127

Available typed array types

Type
Bytes/element
Range
Use case
Int8Array
1
−128 to 127
Signed bytes
Uint8Array
1
0 to 255
Raw bytes, image pixels
Uint8ClampedArray
1
0 to 255 (clamped)
Canvas pixel data
Int16Array
2
−32768 to 32767
Audio samples
Int32Array
4
±2 billion
Integer arithmetic
Float32Array
4
~±3.4×10³⁸
WebGL vertices, 3D
Float64Array
8
±1.8×10³⁰⁸
Scientific computing
BigInt64Array
8
64-bit integers
Large integers

That'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);   // 4

They 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 type
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.