Sub topic 5
What Happens When a Value Does Not Fit
A typed array can only store values its element type can represent. A regular array keeps whatever you put in it. A typed array converts every value on write, and it never throws an error when a value is out of range. This chapter explains the conversion rules so you can predict what gets stored.
It assumes you already know how to create typed arrays and what range each element type covers, from the previous chapter. You should also be comfortable with the difference between integers and floating-point numbers.
The Conversion Pipeline
Every write to a typed array element first converts the value to a Number with the usual ToNumber rules, so "12" becomes 12 and "abc" becomes NaN. The element type then decides what happens next.
Integer Types Wrap
Integer types first drop the fractional part, truncating toward zero. Then they keep only the low n bits, which is the same as taking the value modulo 2^n. Signed types read those bits as two's complement.
console.log(new Uint8Array([255, 256, -1, 1.7])); // Uint8Array [255, 0, 255, 1]
console.log(new Int8Array([127, 128])); // Int8Array [127, -128]
console.log(new Int8Array([200, -1.9])); // Int8Array [-56, -1]
console.log(new Uint16Array([NaN, Infinity, "7"])); // Uint16Array [0, 0, 7]Note that -1.9 truncates to -1, not -2. The conversion does not use Math.floor.
Uint8ClampedArray Saturates
Uint8ClampedArray was designed for canvas pixel data. A pixel channel that overflows should stay at full brightness, not wrap around to black. Values below 0 become 0, values above 255 become 255, and fractions round to the nearest integer. A tie rounds to the nearest even number.
console.log(new Uint8ClampedArray([300, -5, 1.5])); // Uint8ClampedArray [255, 0, 2]
console.log(new Uint8ClampedArray([2.5, NaN])); // Uint8ClampedArray [2, 0]Float Types Lose Precision
Float64Array stores exactly what a JavaScript Number holds. Float32Array rounds to the nearest 32-bit float, which keeps about 7 significant decimal digits.
const f = new Float32Array([0.1, 16777217]);
console.log(f[0]); // 0.10000000149011612
console.log(f[1]); // 16777216Comparing f[0] === 0.1 gives false. To compare against a Float32 value, round the literal the same way first with Math.fround(0.1).
Common Mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Summing bytes into a `Uint8Array` accumulator | Totals wrap to small numbers | Accumulate in a plain number or `Uint32Array` |
| Using `Uint8Array` for pixel math | Bright pixels turn dark | Use `Uint8ClampedArray` or clamp manually |
| Expecting rounding from integer types | `2.9` stored as `2` | Call `Math.round` before writing |
| Comparing `Float32Array` values with `===` | Equality fails | Compare with `Math.fround(x)` |
Exercise
What does new Int16Array([40000, -1.9, NaN]) contain, and what does new Uint8ClampedArray([254.5, 255.5]) contain?
Show answer
The Int16Array holds [-25536, -1, 0]. 40000 wraps to 40000 - 65536 = -25536, -1.9 truncates toward zero to -1, and NaN becomes 0. The Uint8ClampedArray holds [254, 255]. 254.5 is a tie, so it rounds to the even value 254, and 255.5 clamps to 255.
Exercise
You average two pixel values with out[i] = (a[i] + b[i]) / 2, where out is a Uint8Array, a[i] is 200 and b[i] is 101. What is stored, and does the sum overflow?
Show answer
150 is stored. a[i] + b[i] is computed as an ordinary Number (301), so nothing overflows there. The result 150.5 truncates to 150 on write. Overflow would only happen if the intermediate sum were stored in the Uint8Array first.