Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

TypedArray: sizes, indexing, views, and copies

Learn the difference between element counts and byte counts in TypedArray. See how buffer and byteOffset describe a view and why subarray() shares data while slice() creates an independent copy.

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

const view = new Uint16Array(buffer, byteOffset, length);
view.length; view.byteLength; view.buffer; view.byteOffset;
view[index]; view[index] = value;
view.subarray(begin, end);
view.slice(start, end);

TypedArray is the common interface for typed views of binary data, not a globally available constructor. Use a concrete type such as Uint16Array. These APIs belong to ECMAScript and require no DOM. Here, TypedArray.length, TypedArray.byteLength, TypedArray.buffer, and TypedArray.byteOffset denote instance properties such as view.length, not static properties.

length counts elements, byteLength counts the view's bytes, buffer references the entire underlying ArrayBuffer or SharedArrayBuffer, and byteOffset locates the view's start in bytes relative to the buffer. For a valid view, byteLength === length * BYTES_PER_ELEMENT. A view can cover only part of its buffer. In new Uint16Array(buffer, byteOffset, length), the offset must be a multiple of 2, while length still counts elements. These properties are read-only; resizing the buffer can nevertheless change reported sizes, and an out-of-bounds view reports zero sizes and offset.

TypedArray.indexing denotes the syntax view[index], not a method. Indices start at zero and address elements relative to the view's start. Assignment converts the value to the element type. An out-of-bounds read returns undefined; an out-of-bounds write neither grows the view nor stores a value. view[-1] does not read the last element.

TypedArray.prototype.subarray(begin, end) creates a new view sharing the buffer: changes to overlapping elements are visible through both views. TypedArray.prototype.slice(start, end) copies the selected elements into an independent buffer. Both methods count boundaries in elements, include the start, and exclude the end; negative boundaries count from the end, and an omitted end means the view's end. These rules describe ordinary built-in types; custom subclasses can alter result construction through Symbol.species.

Examples

const buffer = new ArrayBuffer(12);
const view = new Uint16Array(buffer, 2, 4);
view.set([10, 20, 30, 40]);
const shared = view.subarray(1, 3);
const copied = view.slice(1, 3);
shared[0] = 99;
console.log(view.length, view.byteLength, view.byteOffset, buffer.byteLength);
console.log(shared.byteOffset, shared.buffer === view.buffer, copied.buffer === view.buffer);
console.log(view.join(','));
console.log(shared.join(','));
console.log(copied.join(','));
view[4] = 50;
console.log(view[4], view.length);

Result

4 8 2 12
4 true false
10,99,30,40
99,30
20,30
undefined 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