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.
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
Type
Bytes/element
Range
Use case
Int8Array1
−128 to 127
Signed bytes
Uint8Array1
0 to 255
Raw bytes, image pixels
Uint8ClampedArray1
0 to 255 (clamped)
Canvas pixel data
Int16Array2
−32768 to 32767
Audio samples
Int32Array4
±2 billion
Integer arithmetic
Float32Array4
~±3.4×10³⁸
WebGL vertices, 3D
Float64Array8
±1.8×10³⁰⁸
Scientific computing
BigInt64Array8
64-bit integers
Large integers
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 type
Try It Yourself
Official 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.