Typed Arrays
A typed array is an array-like view onto a block of raw binary memory, where every item is a number of one fixed type, such as an unsigned 8-bit integer or a 32-bit float. Typed arrays exist for handling binary data efficiently: image pixels on a canvas, audio samples, WebGL buffers, files read as bytes, and binary messages sent over fetch() or WebSockets. For an ordinary list of values, use a normal array.
Creating a typed array
Section titled “Creating a typed array”A typed array has a fixed length, and every item starts at 0. Values written to it are converted to the array’s type:
const bytes = new Uint8Array(4);console.log(bytes);
bytes[0] = 200;bytes[1] = 300;bytes[2] = -1;bytes[3] = 3.9;console.log(bytes);console.log(bytes.length, 'push' in bytes);This prints:
Uint8Array(4) [ 0, 0, 0, 0 ]Uint8Array(4) [ 200, 44, 255, 3 ]4 falseA Uint8Array holds whole numbers from 0 to 255. A value outside that range wraps round, so 300 becomes 44 (300 minus 256) and -1 becomes 255, and a fraction loses its decimal part. Because the length is fixed, there is no push(), pop(), shift(), unshift() or splice().
The types
Section titled “The types”| Type | Bytes per item | Values |
|---|---|---|
Int8Array |
1 | -128 to 127 |
Uint8Array |
1 | 0 to 255 |
Uint8ClampedArray |
1 | 0 to 255, clamped |
Int16Array |
2 | -32,768 to 32,767 |
Uint16Array |
2 | 0 to 65,535 |
Int32Array |
4 | -2,147,483,648 to 2,147,483,647 |
Uint32Array |
4 | 0 to 4,294,967,295 |
Float16Array |
2 | half-precision floating point, up to ±65,504 |
Float32Array |
4 | single-precision floating point, up to about ±3.4 × 10³⁸ |
Float64Array |
8 | double-precision floating point, the same as a normal JavaScript number |
BigInt64Array |
8 | -2⁶³ to 2⁶³ - 1, as BigInt values |
BigUint64Array |
8 | 0 to 2⁶⁴ - 1, as BigInt values |
Float16Array was added in ES2025 and is supported in current browsers and Node 24 or later.
Uint8ClampedArray differs from Uint8Array in how it handles values out of range. Instead of wrapping round, it clamps them to 0 or 255, which is what pixel data needs, and it rounds fractions to the nearest whole number, with halves going to the even neighbour:
const wrapped = new Uint8Array([250, 260, -6]);const clamped = new Uint8ClampedArray([250, 260, -6, 1.5, 2.5]);const signed = new Int8Array([127, 128, -129]);
console.log(wrapped);console.log(clamped);console.log(signed);This prints:
Uint8Array(3) [ 250, 4, 250 ]Uint8ClampedArray(5) [ 250, 255, 0, 2, 2 ]Int8Array(3) [ 127, -128, 127 ]ArrayBuffer and views
Section titled “ArrayBuffer and views”A typed array does not store its own data. The bytes live in an ArrayBuffer, and the typed array is a view that reads them as numbers of its type. Several views can share one buffer and read the same bytes in different ways:
const buffer = new ArrayBuffer(8);const asBytes = new Uint8Array(buffer);const asInt32 = new Int32Array(buffer);
asInt32[0] = 258;console.log(buffer.byteLength);console.log(asBytes);console.log(asInt32.length);This prints:
8Uint8Array(8) [ 2, 1, 0, 0, 0, 0, 0, 0]2The eight bytes hold two 32-bit integers. Writing 258 through the Int32Array sets the first four bytes, and the Uint8Array shows them one at a time. The low byte comes first because typed arrays use the byte order of the device, and almost every current device is little-endian.
Since ES2024, an ArrayBuffer can also be created resizable, with new ArrayBuffer(length, { maxByteLength }), and grown later with resize().
Reading mixed data with DataView
Section titled “Reading mixed data with DataView”A DataView reads and writes values of any type at any byte offset in a buffer, and lets you choose the byte order. That makes it the tool for binary file formats and network protocols, where one record mixes several types:
const buffer = new ArrayBuffer(6);const view = new DataView(buffer);
view.setUint16(0, 258);view.setUint16(2, 258, true);view.setInt8(4, -1);
console.log(new Uint8Array(buffer));console.log(view.getUint16(0), view.getUint16(2, true), view.getUint8(4));
const tail = new DataView(buffer, 4, 2);console.log(tail.getInt8(0));This prints:
Uint8Array(6) [ 1, 2, 2, 1, 255, 0 ]258 258 255-1Each method’s last argument selects the byte order. It defaults to big-endian, and passing true selects little-endian, so the same number is stored as 1, 2 at offset 0 and as 2, 1 at offset 2. The byte at offset 4 reads as -1 through getInt8() and as 255 through getUint8(). A DataView can also cover part of a buffer: tail starts at byte 4 and is 2 bytes long, so its offset 0 is the buffer’s byte 4.
Array methods on typed arrays
Section titled “Array methods on typed arrays”Typed arrays have most of the methods of normal arrays, including map(), filter(), find(), sort() and forEach(). Methods that build a new array return a typed array of the same type. Array.from() converts one into a normal array:
const readings = Float32Array.from([0.1, 0.5, 2.25]);console.log(readings);console.log(readings.map((r) => r * 2));console.log(readings.filter((r) => r > 0.4));console.log(Array.from(readings));This prints:
Float32Array(3) [ 0.10000000149011612, 0.5, 2.25 ]Float32Array(3) [ 0.20000000298023224, 1, 4.5 ]Float32Array(2) [ 0.5, 2.25 ][ 0.10000000149011612, 0.5, 2.25 ]A 32-bit float has less precision than a normal number, so 0.1 is stored as the nearest value it can hold. Unlike a normal array, sort() on a typed array sorts numerically without a compare function.
MDN’s JavaScript typed arrays guide covers the APIs that produce and accept them.
Steve Griffith covers this topic in his video Intro to Typed Arrays in JavaScript.