Code Reference

Typed Arrays

JavaScript · Reference cheat sheet

Typed Arrays

JavaScript · Reference cheat sheet


📋 Overview

Typed arrays are array-like views over binary ArrayBuffer (and SharedArrayBuffer) memory. Use them for files, WebGL, WebAudio, WASM, network protocols, and precise integer/float layouts. They fix element types and sizes — no holes, no mixed types.

🔧 Core concepts

TypeBytesDescription
Int8Array / Uint8Array / Uint8ClampedArray18-bit
Int16Array / Uint16Array216-bit
Int32Array / Uint32Array432-bit
Float32Array / Float64Array4 / 8IEEE floats
BigInt64Array / BigUint64Array8BigInt elements
  • ArrayBuffer: raw bytes; view interprets them.
  • DataView: explicit endianness for mixed layouts.
  • buffer / byteOffset / byteLength: view window into buffer.
  • Endianness: typed arrays use platform endianness; DataView lets you choose.
const buf = new ArrayBuffer(8);
const view = new Uint32Array(buf);
view[0] = 0xffffffff;

💡 Examples

// From length / from array
const a = new Uint8Array(4);
const b = new Uint8Array([1, 2, 3, 4]);
const c = new Uint8Array(b); // copy

// Shared buffer, two views
const buffer = new ArrayBuffer(16);
const u8 = new Uint8Array(buffer);
const f32 = new Float32Array(buffer);
f32[0] = 1.5;
u8.slice(0, 4); // bytes of that float

// Subarray (view) vs slice (copy)
const mid = u8.subarray(2, 6); // shares buffer
const copy = u8.slice(2, 6); // new buffer

// DataView for protocols
const dv = new DataView(buffer);
dv.setUint16(0, 0x1234, false); // big-endian
dv.getUint16(0, false);

// Bytes from string
const enc = new TextEncoder().encode("hi");
const dec = new TextDecoder().decode(enc);

// Fetch as bytes
const res = await fetch("/file.bin");
const bytes = new Uint8Array(await res.arrayBuffer());
// Clamped for canvas ImageData
const clamped = new Uint8ClampedArray([300, -1, 128]);
// [255, 0, 128]

⚠️ Pitfalls

  • Out-of-range assigns wrap (or clamp for Uint8ClampedArray) — no throw.
  • TypedArray is not a real Array — no push/pop; some methods differ.
  • Endian bugs when exchanging binary across architectures — prefer DataView for wire formats.
  • Detached buffers (after postMessage transfer / WASM) throw on access.
  • Align byteOffset to element size for multi-byte typed arrays.

On this page