← Back to JavaScript Course | Chapter 3: Strings, Arrays & Data | Lesson 6 of 6

JS Typed Arrays

A regular JavaScript array is like a flexible shopping bag that can hold anything of any size, growing or shrinking as needed; a typed array is more like a labeled egg carton, a fixed number of slots that each hold exactly one specific kind of number. That rigidity is the whole point: it lets JavaScript read and write binary data as efficiently as lower-level languages do.

What Is a Typed Array

A typed array is a fixed-length, fixed-type array-like object backed by raw binary memory, used for working directly with numeric data such as pixel values, audio samples, or network packets. Unlike a regular array, you cannot push or pop elements, since its length is set once when it's created.

Example: What Is a Typed Array

javascript
const arr = new Int8Array(3);
arr[0] = 5;
console.log(arr); // fixed length, no push/pop

ArrayBuffer and Views

An ArrayBuffer is a chunk of raw, untyped binary memory with no way to read or write it directly; a typed array is a view that interprets those raw bytes as numbers of a specific type. Multiple views of different types can share the exact same underlying ArrayBuffer, interpreting the same bytes differently.

Example: ArrayBuffer and Views

javascript
const buffer = new ArrayBuffer(4);
const view = new Int32Array(buffer);
view[0] = 42;
console.log(view[0]);

Common Typed Array Types

JavaScript provides several typed array constructors covering different sizes and signedness, including Int8Array and Uint8Array for single bytes, Int16Array and Int32Array for larger whole numbers, and Float32Array and Float64Array for decimals. Choosing the right type balances memory usage against the range of values you need to store.

Example: Common Typed Array Types

javascript
const bytes = new Uint8Array(2);
const ints = new Int16Array(4);
const floats = new Float32Array(2);
console.log(bytes.length, ints.length, floats.length);

DataView for Mixed-Type Access

DataView gives fine-grained, explicit control over reading and writing values at specific byte offsets within an ArrayBuffer, including choosing little-endian or big-endian byte order per operation. It's the right tool when parsing a binary file format or network protocol that mixes different data types at fixed positions.

Example: DataView for Mixed-Type Access

javascript
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setInt16(0, 300, true); // little-endian
console.log(view.getInt16(0, true));

Why Use Typed Arrays

Typed arrays exist because JavaScript needed a way to handle binary data efficiently for tasks like WebGL graphics, audio processing, WebSockets, and file parsing, where a regular flexible array would be far slower and use more memory. They're a lower-level tool aimed at performance-sensitive or binary-protocol code, not everyday application logic.

Example: Why Use Typed Arrays

javascript
const pixelData = new Uint8ClampedArray([255, 0, 0, 255]);
console.log(pixelData);
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.