Typed arrays and numeric conversion
Quick answer
Typed arrays expose binary memory as fixed-width numeric elements. Their element type determines storage size and conversion rules.
How it works
Uint8Array stores unsigned 8-bit integers; Int16Array stores signed 16-bit integers; Float32Array stores single-precision floats. length counts elements, byteLength counts bytes. Integer views convert values to their representable range; unsigned 8-bit assignment wraps modulo 256 after integer conversion. Uint8ClampedArray instead clamps and rounds. BigInt typed arrays require BigInt values.
Local examples
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const a=new Uint8Array([-1,256,260]);console.log(JSON.stringify([...a]));})()Expected console output:
[255,0,4]Unsigned bytes wrap to the 0–255 range.
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const a=new Uint8ClampedArray([-1,260,2.5,3.5]);console.log(JSON.stringify([...a]));})()Expected console output:
[0,255,2,4]Clamping differs from wrapping; halves round to even.
Common mistakes
Typed arrays are not ordinary arrays: push is absent, and assigning out-of-range numeric indices does not extend them.
Practice
Store -1, 256 and 260 in Uint8Array and Uint8ClampedArray and compare the results.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Typed arrays and numeric conversion — Knowledge Check
Apply the concept and reason about local example output.
How many bytes does a two-element Int16Array occupy?
Which view clamps out-of-range numeric inputs instead of wrapping?
Can push extend an ordinary fixed-length typed array?
What is the expected console output of Typed arrays and numeric conversion example 1?
(async () => {const a=new Uint8Array([-1,256,260]);console.log(JSON.stringify([...a]));})()What is the expected console output of Typed arrays and numeric conversion example 2?
(async () => {const a=new Uint8ClampedArray([-1,260,2.5,3.5]);console.log(JSON.stringify([...a]));})()