Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Uint8Array — an unsigned byte view

Uint8Array exposes buffer data as unsigned 8-bit integers ranging from 0 to 255. It lets you create bytes, copy them, or share memory through views; out-of-range writes wrap rather than clamp.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

new Uint8Array(length)
new Uint8Array(values)
new Uint8Array(buffer[, byteOffset[, length]])

Uint8Array is a typed array: a buffer view whose elements each occupy one byte and read back as Number values from 0 to 255. It is not an ordinary Array. Its length property counts elements, byteLength gives the view's size in bytes, and buffer exposes the underlying buffer.

new Uint8Array(length) creates a zero-filled buffer. new Uint8Array(values) copies values from an iterable or array-like object. new Uint8Array(buffer, byteOffset, length) creates a view of an existing buffer without copying; byteOffset is an offset in bytes, and length counts elements. The constructor requires new.

On assignment, finite numbers are truncated toward zero and then reduced modulo 256: 256 becomes 0, and -1 becomes 255. NaN and infinities become 0. This is neither range validation nor clamping as in Uint8ClampedArray; validate invalid input before writing it.

subarray() creates a view sharing memory, whereas slice() creates an independent copy. In the example, changing view[0] also changes bytes[1], but leaves copy unchanged. This entry covers core ECMAScript operations, not newer encoding methods. The specification alone does not establish compatibility with a particular browser; Node 24 being present in a repository does not establish it either.

Examples

const bytes = new Uint8Array([0, 255, 256, -1]);
const view = bytes.subarray(1, 3);
const copy = bytes.slice(1, 3);
view[0] = 42;
console.log(bytes.join(','));
console.log(copy.join(','));
console.log(bytes.length + ':' + bytes.byteLength);

Result

0,42,0,255
255,0
4:4

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources