Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Typed Arrays and Binary Data

Typed arrays

Reference for JavaScript typed arrays: Uint8Array, Int32Array, Float64Array and more, with constructors, overflow rules, subarray vs slice and live examples.

Typed arrays are array-like views over an ArrayBuffer. Every element has the same numeric type and a fixed size in bytes, so the data is compact and fast to process. Each type has its own constructor: Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, BigInt64Array and BigUint64Array.

Unlike regular arrays, a typed array has a fixed length and cannot hold anything but numbers of its type.

JavaScript
const pixels = new Uint8Array(4);
const samples = Float64Array.from([0.5, 1.5, 2.5]);

Try it

JS Creating and overflowing

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Views, subarray and slice

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
new Uint8Array()                        // empty
new Uint8Array(length)
new Uint8Array(arrayOrIterable)
new Uint8Array(typedArray)
new Uint8Array(buffer, byteOffset, length)

Uint8Array.from(source, mapFn)
Uint8Array.of(1, 2, 3)
Argument Description
length Number of elements; all start as 0.
arrayOrIterable Values are converted to the element type.
buffer An ArrayBuffer (or SharedArrayBuffer) to view.
byteOffset Where the view starts; must be a multiple of the element size. Defaults to 0.
length (with a buffer) Element count. If omitted, the view extends to the end of the buffer.

Element types

Constructor Bytes Range or kind
Int8Array / Uint8Array 1 -128..127 / 0..255
Uint8ClampedArray 1 0..255, clamps and rounds
Int16Array / Uint16Array 2 -32768..32767 / 0..65535
Int32Array / Uint32Array 4 32-bit signed / unsigned
Float32Array / Float64Array 4 / 8 IEEE 754 floating point
BigInt64Array / BigUint64Array 8 BigInt values only

Properties: length, byteLength, byteOffset, buffer, and BYTES_PER_ELEMENT (on the instance and the constructor).

Exceptions: RangeError for a bad length, a misaligned byteOffset or a view that does not fit in the buffer; TypeError when mixing BigInt and Number values with the BigInt arrays.

Usage notes

  • Many array methods exist, including map(), filter(), reduce(), find(), fill(), join(), includes(), indexOf(), reverse() and toSorted(), but there is no push(), pop(), splice() or concat(), because the length is fixed.
  • Typed-array-only methods: set(source, offset) copies values in, subarray(begin, end) creates a new view of the same memory.
  • subarray() shares, slice() copies. This is the most common source of surprises.
  • Writes are converted, not rejected. Out-of-range integers wrap (the clamped array clamps), fractions are truncated, and NaN becomes 0 in integer arrays.
  • Out-of-range indexes are ignored. Writing a[10] on a three-element array does nothing and reading it gives undefined.
  • sort() is numeric by default, unlike Array.prototype.sort(), which compares as text.
  • Byte order follows the machine. Use DataView for explicit endianness.
  • Plain array conversion: Array.from(typed) or [...typed].
  • Float16Array is a recent addition with limited support.

Browser support

The classic typed arrays work in every current browser. Newer pieces such as Float16Array and toSorted()-style methods on typed arrays are more recent; check a compatibility table.