CKTIS / JavaScript
Uint8ClampedArray — bytes with clamped values
Uint8ClampedArray is a typed array storing integers from 0 to 255. Writes clamp values to that range and round fractions, rather than wrapping values as Uint8Array does.
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 Uint8ClampedArray(length)
new Uint8ClampedArray(values)
new Uint8ClampedArray(buffer)
new Uint8ClampedArray(buffer, byteOffset)
new Uint8ClampedArray(buffer, byteOffset, length)Uint8ClampedArray is a standard ECMAScript type: a buffer view whose elements each occupy one byte and represent unsigned integers from 0 to 255. Values read from it have type Number; it is not an ordinary Array.
Writes apply ToUint8Clamp: values below 0 become 0, and values above 255 become 255. Fractions round to the nearest integer; exact ties go to the even integer, so 2.5 becomes 2 and 3.5 becomes 4. This differs from Math.round. NaN becomes 0, Infinity becomes 255, and -Infinity becomes 0. Uint8Array instead truncates fractions and wraps integer values modulo 256.
new Uint8ClampedArray(length) creates zero-filled elements, while new Uint8ClampedArray(values) copies and converts values. The buffer form creates a view over existing memory without copying; creating the view alone does not convert the stored bytes. The constructor requires new.
The example demonstrates clamping and rounding during construction and subsequent assignment. Clamping applies to element writes, not calculations performed on numbers read from the array. This is a language feature, not a browser API; the specification is not a browser compatibility table.
Examples
const values = new Uint8ClampedArray([-20, 2.5, 3.5, 300, NaN]);
values[0] = 260;
console.log(JSON.stringify(Array.from(values)));Result
[255,2,4,255,0]
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.