Typed Arrays and ArrayBuffer

Advanced · 17 min

`ArrayBuffer` as raw binary memory, the typed array views that give its bytes a numeric shape, and `DataView` for reading mixed types at byte offsets.

Before thisArrays and Array Methods

Logan Lee · 17 min

Typed Arrays and ArrayBuffer

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Logan LeeReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An `ArrayBuffer` is a fixed-length block of raw memory and nothing else — you can create one (`new ArrayBuffer(16)` for 16 bytes) but you cannot read or write its bytes directly; it only becomes usable through a view laid on top of it.
  • A typed array (`Uint8Array`, `Int16Array`, `Float64Array`, and others) is a view that interprets an `ArrayBuffer`'s bytes as a sequence of same-sized numbers of one specific type — array-like syntax (`view[0]`, a `.length`) over memory that's really just bytes underneath.
  • `DataView` is a second, more flexible kind of view over the same buffer: instead of committing to one numeric type for the whole buffer, it lets you read and write individual values of different types and sizes at explicit byte offsets, with explicit control over byte order (endianness).
  • Typed arrays are not true arrays — `Array.isArray(new Uint8Array())` is `false`, their length is fixed at creation (no `push`, `pop`, or `splice`), and every element is coerced to the view's declared numeric type, which is why these exist as a distinct family instead of being folded into regular arrays.
  • The reason to reach for any of this: performance (fixed-width numeric memory the engine can lay out and process efficiently, without the overhead of a general-purpose array of boxed values) and interop (binary data from `fetch`'s `arrayBuffer()`, the Web Audio API, `<canvas>` pixel data, and WebSocket binary frames all hand you an `ArrayBuffer` directly, with no JSON or text in between).

Where a regular array stops being the right tool

Everything in the arrays lesson is built on a flexible, general-purpose structure — an array can hold any mix of types, grow or shrink freely, and store its elements as full JavaScript values with all the overhead that implies. That flexibility is exactly what you don’t want in two specific situations: when you’re handling genuinely binary data (an image file, an audio buffer, bytes coming off a network socket) that has no business being wrapped in general-purpose objects, and when raw performance on large numeric data actually matters, because a regular array of numbers still stores each one as a full, individually-boxed value rather than a tight, fixed-width block of memory. Typed arrays and ArrayBuffer exist for exactly this gap.

ArrayBuffer: memory, and only memory

An ArrayBuffer is about as simple as a JavaScript object gets: a fixed-length block of raw memory, and nothing else.

const buffer = new ArrayBuffer(16); // 16 bytes, all zeroed out
buffer.byteLength; // 16

You cannot read a value out of buffer or write one into it directly — there’s no buffer[0] that means anything. All an ArrayBuffer commits to is “this many bytes exist.” To do anything with those bytes, you need a view — either a typed array or a DataView — layered on top of the same underlying memory.

Typed arrays: giving the bytes a uniform shape

A typed array is a view that commits an entire buffer to one specific numeric type, then gives you array-like access to it:

const buffer = new ArrayBuffer(4);
const ints = new Uint8Array(buffer);
ints[0] = 255;
ints[1] = 16;
console.log(ints[0], ints[1]); // 255 16
console.log(ints.length);      // 4 — one element per byte, since Uint8Array is 8 bits wide

Uint8Array reads each byte in the buffer as its own unsigned 8-bit integer. Other constructors interpret the same kind of buffer at different widths and signedness — Int16Array groups bytes two at a time as signed 16-bit integers, Float64Array groups them eight at a time as 64-bit floating-point numbers, and so on through the full family (Uint8Array, Int8Array, Uint16Array, Int16Array, Uint32Array, Int32Array, Float32Array, Float64Array, plus a couple of more specialized ones). You pick the constructor that matches the actual format of the data you’re holding.

This is also why typed arrays aren’t actual arrays, despite the name and the [] syntax: Array.isArray(ints) returns false, ints.length is fixed the moment the view is created (no push, pop, or splice), and every value you assign gets coerced into the view’s declared type — assign 300 into a Uint8Array slot and you’ll get 44 back, because 300 doesn’t fit in 8 bits and wraps around. A typed array is array-shaped sugar over memory, not a general-purpose collection.

DataView: when the layout isn’t uniform

A typed array assumes the whole buffer is one type. That assumption breaks the moment you’re handling a format with a mixed layout — a binary file header with, say, a 2-byte version field followed by a 4-byte timestamp and then a chunk of raw pixel data. DataView is built for exactly that: instead of committing to one type for the whole buffer, it lets you read or write individual values of whatever type and size you need, at whatever byte offset you specify.

const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);

view.setUint16(0, 1);       // bytes 0-1: a 16-bit value
view.setUint32(2, 1699999); // bytes 2-5: a 32-bit value, starting right after

view.getUint16(0); // 1
view.getUint32(2); // 1699999

DataView also gives you explicit control over byte order — whether the most significant byte of a multi-byte number comes first (big-endian) or last (little-endian) — which matters the moment you’re reading a binary format defined by something other than your own JavaScript code, since not every format agrees on an order and getting it wrong silently produces the wrong number rather than an error.

Where you’d actually run into this

This isn’t abstract plumbing — it’s the data type sitting behind several everyday browser and network APIs. Calling .arrayBuffer() on a fetch Response hands you the raw bytes of whatever you downloaded, ready for a typed array or DataView to interpret. The Web Audio API decodes sound into typed-array sample data. A <canvas>’s pixel data comes back as a typed array of color channel values. WebSocket connections can send and receive binary frames as an ArrayBuffer directly, skipping JSON or text entirely when the payload is genuinely binary. In every one of these, the API is handing you memory, not a convenient object — and typed arrays and DataView are the two tools for turning that memory into something you can actually use.

Where this fits

Typed arrays and ArrayBuffer round out the “Collections and iteration” group as the one place in this group where JavaScript steps outside its usual general-purpose values and deals with memory directly, the way a lower-level language normally would. You won’t reach for this in most everyday application code — regular arrays and objects cover that — but the moment binary data shows up, whether from a file, a socket, or a media API, this is the vocabulary for working with it correctly instead of fighting it through string conversions or generic arrays that were never built for the job.

Check yourself

  1. 01You create `const buf = new ArrayBuffer(8)`. Can you read or write a value into `buf` directly? Why or why not?

    No. An `ArrayBuffer` is only a fixed-length block of raw memory — it defines how many bytes exist, nothing about how to interpret them. To actually read or write anything, you need a view on top of it, either a typed array (which commits to one numeric type for the whole buffer) or a `DataView` (which lets you choose the type per read or write).

  2. 02What's the practical difference between `new Uint8Array(buffer)` and `new Float64Array(buffer)` over the same underlying `ArrayBuffer`?

    Both are views over the identical bytes, but they interpret those bytes completely differently: `Uint8Array` reads each byte as its own unsigned 8-bit integer (0–255), while `Float64Array` groups every 8 bytes together and reads them as one 64-bit floating-point number. Same memory, different shape — which is why picking the right typed array constructor for your data's actual format matters.

  3. 03Why would you reach for a `DataView` instead of a typed array when working with the same `ArrayBuffer`?

    A typed array commits the entire buffer to one fixed numeric type and element width. `DataView` is for when a buffer's layout is mixed or externally defined — say, a binary file format with a 2-byte header field followed by a 4-byte value — where you need to read or write specific types at specific byte offsets, with control over byte order, rather than treating the whole buffer uniformly.

  4. 04Why does `Array.isArray(new Uint8Array([1, 2, 3]))` return `false`, and what's one regular array method a typed array doesn't have?

    A typed array is a distinct kind of object, not a specialization of the regular `Array` type the way, say, arguments objects are sometimes conflated with arrays — `Array.isArray` is checking for the actual `Array` type specifically, and a typed array isn't it. Typed arrays also have a length fixed at creation, so methods that change an array's length, like `push`, `pop`, or `splice`, don't exist on them at all.

  5. 05What's one real situation where you'd actually receive an `ArrayBuffer` without asking for typed arrays specifically?

    Calling `.arrayBuffer()` on a `fetch` `Response` — for downloading an image, a font, or any other binary file — hands you the response body as a raw `ArrayBuffer`, which you then view with the right typed array or `DataView` depending on what the binary format actually is. The Web Audio API's decoded audio data and `<canvas>` pixel buffers are two more examples of APIs that hand you binary data this way instead of JSON or text.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark