CKTIS / JavaScript
Uint16Array — unsigned 16-bit typed array
Uint16Array is a typed buffer view storing integers from 0 to 65535, using 2 bytes per element. Learn how values are converted and how copying data differs from sharing a view.
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 Uint16Array(length)
new Uint16Array(iterableOrArrayLike)
new Uint16Array(typedArray)
new Uint16Array(buffer, byteOffset?, length?)Uint16Array is a built-in ECMAScript typed array, not an ordinary Array. Each element occupies 2 bytes and reads back as a Number from 0 to 65535. length counts elements, byteLength counts the view's bytes, and Uint16Array.BYTES_PER_ELEMENT is 2.
The constructor requires new. For a numeric argument, the constructor uses ToIndex to determine the number of zero-filled elements. For example, new Uint16Array(3.9) creates three elements, while new Uint16Array(-1) throws a RangeError. A collection supplies values to copy. The ArrayBuffer overload creates a view without copying: byteOffset is measured in bytes and must be a multiple of 2, while optional length counts elements, not bytes.
Writes convert values rather than validate their range: finite Number values are truncated toward zero, then reduced modulo 65536. Thus 65536 becomes 0, -1 becomes 65535, and 3.9 becomes 3. NaN and infinities become 0; BigInt values cause TypeError. This is not clamping.
subarray() shares storage with the original, whereas slice() copies data. The example demonstrates conversion and modification of the original through a view. Byte order is implementation-native; use DataView for a binary format with explicit byte order.
Examples
const values = new Uint16Array([65535, 65536, -1, 3.9]);
const view = values.subarray(1, 3);
view[0] = 42;
console.log(values.join(","));
console.log(`${values.length} ${values.byteLength} ${Uint16Array.BYTES_PER_ELEMENT}`);Result
65535,42,65535,3 4 8 2
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.