Difference between Uint8Array and Uint8ClampedArray Difference between Uint8Array and Uint8ClampedArray arrays arrays

Difference between Uint8Array and Uint8ClampedArray


Looking at the examples for Uint8ClampedArray and Uint8Array, it looks like the difference is how values are treated when assigned.

If you are trying to set one element to a clamped array to any value outside of the range 0-255, it will simply default to 0 or 255 (depending on whether the value is smaller or larger). A normal Uint8Array array just takes the first 8 bit of the value.

Examples:

var x = new Uint8ClampedArray([17, -45.3]);console.log(x[0]); // 17console.log(x[1]); // 0console.log(x.length); // 2var x = new Uint8Array([17, -45.3]);console.log(x[0]); // 17console.log(x[1]); // 211console.log(x.length); // 2