Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Binary data and streams

ArrayBuffer stores bytes, while typed arrays and DataView interpret them. Learn about shared memory, byte order, and the distinction between an in-memory buffer and a stream.

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 ArrayBuffer(byteLength)
new Uint8Array(buffer, byteOffset, length)
new DataView(buffer, byteOffset, byteLength)
view.setUint16(byteOffset, value, littleEndian)
view.getUint16(byteOffset, littleEndian)

ArrayBuffer stores binary data but does not provide indexed access to its bytes. Views provide that access: Uint8Array interprets each byte as a number from 0–255, while other typed arrays group bytes into elements of a particular type. A typed array's length counts elements; byteLength counts bytes.

DataView reads and writes values of different types at byte offsets. For multibyte values, it lets you explicitly select byte order: false means big-endian and true means little-endian. Typed arrays use the environment's byte order, so use DataView for formats with a specified byte order.

Views constructed from the same buffer can cover different byte ranges. A write through one view is visible through another only for the affected bytes that the other view covers. subarray() creates another view, whereas a typed array's slice() copies elements. The example writes two numbers and shows the same data as bytes.

A buffer represents data in memory; a stream delivers or receives successive chunks. A chunk boundary is not necessarily a record boundary. Stream APIs belong to the environment, not core ECMAScript. The example uses features available since ES2015, without stream APIs; working in Node.js 24 does not establish browser API support.

Examples

const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setUint16(0, 0x1234, false);
view.setUint16(2, 0xABCD, false);
const bytes = new Uint8Array(buffer);
console.log(Array.from(bytes).join(","));
console.log(view.getUint16(0, false));

Result

18,52,171,205
4660

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