JavaScript Notes
Master JavaScript Typed Arrays: ArrayBuffer, DataView, Int8Array, Uint8Array, Float32Array, and more. Learn binary data handling, WebGL, WebAudio, File API, and typed array interview Q&A.
Typed Arrays are fixed-length, typed views over raw binary memory buffers. Unlike regular JavaScript arrays (which can hold any type and have dynamic size), Typed Arrays store a single numeric type in contiguous memory — just like arrays in C or C++. This makes them ideal for high-performance numerical work, WebGL, audio processing, and network binary protocols.
💡 Key insight: A Typed Array is a view over an ArrayBuffer (raw bytes). You can have multiple different typed views over the same buffer — like reading the same byte sequence as integers *and* floats.The Architecture
Available Typed Array Types
| Constructor | Bytes | Range |
|---|---|---|
Int8Array | 1 | -128 to 127 |
Uint8Array | 1 | 0 to 255 |
Uint8ClampedArray | 1 | 0–255 (clamps, doesn't wrap) |
Int16Array | 2 | -32768 to 32767 |
Uint16Array | 2 | 0 to 65535 |
Int32Array | 4 | -2^31 to 2^31 - 1 |
Uint32Array | 4 | 0 to 2^32 - 1 |
Float32Array | 4 | ±3.4e38 (single precision) |
Float64Array | 8 | ±1.8e308 (double precision) |
BigInt64Array | 8 | BigInt |
BigUint64Array | 8 | BigInt |
Creating Typed Arrays
Uint8Array [0, 0, 0, 0] 4 Int32Array [10, 20, 30, 40] 3.14 8 Int32Array [1, 2, 3]
ArrayBuffer — The Raw Bytes
16 true "78" "56" "34" "12"
DataView — Precise Binary Control
DataView allows reading/writing arbitrary types at any byte offset, with explicit endianness control:
const buffer = new ArrayBuffer(12);
const view = new DataView(buffer);
view.setInt32(0, 300, true); // offset 0, value 300, little-endian
view.setFloat32(4, 3.14, true); // offset 4, 3.14, little-endian
view.setUint8(8, 255); // offset 8, 255
console.log(view.getInt32(0, true)); // 300
console.log(view.getFloat32(4, true).toFixed(2)); // "3.14"
console.log(view.getUint8(8)); // 255300 "3.14" 255
Typed Array Methods
Typed Arrays support most Array methods:
Float64Array [1, 2, 3, 5, 8, 9] [5, 8, 9] [2, 4, 6, 10, 16, 18] Float64Array [0, 0, 10, 20, 30, 0] Float64Array [2, 3, 5] Float64Array [2, 3, 5]
Practical: Canvas Pixel Manipulation
"Greyscale filter applied" // Canvas now shows a greyscale version of the original image
Typed Array vs Regular Array
| Typed Array | Regular Array | |
|---|---|---|
| Element type | Fixed (e.g., Float32 only) | Any mixed type |
| Size | Fixed at creation | Dynamic |
| Memory | Contiguous, C-style | Scattered, boxed |
| Performance | ✅ 10-100x faster for numbers | Slower for raw numbers |
| Methods | Subset of Array methods | Full Array API |
| Use case | WebGL, audio, binary protocols, canvas | General purpose |
When to Use Typed Arrays
- Canvas / image processing —
Uint8ClampedArrayfor pixel RGBA data - WebGL —
Float32Arrayfor vertex buffers,Uint16Arrayfor index buffers - Web Audio API —
Float32Arrayfor audio sample processing - Binary file parsing — reading binary formats (BMP, WAV, custom protocols)
- WebSocket binary messages — processing binary frames
- Shared memory —
SharedArrayBufferfor cross-worker binary communication
Common Mistakes
- Writing out-of-range values — typed arrays truncate, not throw.
new Uint8Array([256])[0]is0(wraps around), not 256. - Modifying a
subarrayaffects the original —subarrayis a view, not a copy. Useslicefor an independent copy. - Using
filter/mapexpecting a TypedArray back — these return regular Arrays. Usenew TypedArray(array.filter(...))to get a TypedArray back. - Ignoring endianness in DataView — always specify the endian parameter (
true= little-endian) for portable code.
Interview Questions
Q1. What is an ArrayBuffer?
An ArrayBuffer is a fixed-length raw binary data buffer in memory. It cannot be read directly — you must create a Typed Array view or DataView to read/write its contents.Q2. What is the difference between slice and subarray?
slice(start, end)returns a new TypedArray with copied data — changes to the slice don't affect the original.subarray(start, end)returns a view over the same underlyingArrayBuffer— changes to the subarray affect the original.
Q3. Why are Typed Arrays faster than regular arrays for numeric operations?
Typed Arrays store values as raw binary in contiguous memory — no boxing overhead, no type checking per element. JavaScript engines can use SIMD instructions and optimised loops. Regular arrays store boxed objects that need dereferencing.
Q4. What is Uint8ClampedArray and where is it used?
Uint8ClampedArrayis likeUint8Arraybut instead of wrapping out-of-range values (256 → 0), it clamps them to the nearest boundary (256 → 255, -1 → 0). It's used for pixel data in Canvas (ImageData.data) where you want values to stay in the 0–255 range.
Q5. How do you share an ArrayBuffer between two typed array views?
Pass the same ArrayBuffer to both typed array constructors. Both will read/write the same underlying bytes. Writes through one view are immediately visible through the other.Q6. What is DataView and when do you need it?
DataViewprovides fine-grained access to anArrayBufferat any byte offset and with explicit endianness. Use it when parsing binary formats where different data types appear at arbitrary offsets, or when endianness control is required.
Q7. What happens when you write a value out of range to a Typed Array?
The value is silently truncated (modulo).new Uint8Array(1)[0] = 300stores44(300 % 256).Uint8ClampedArrayclamps instead of wrapping. No error is thrown.
Key Takeaways
- Typed Arrays are fixed-length, single-type views over raw
ArrayBuffermemory — much faster for numeric work than regular arrays. - Use
slice()for a copy;subarray()for a view (shares the same buffer). DataViewgives per-byte control with explicit endianness — essential for binary protocol parsing.Uint8ClampedArrayclamps to 0–255 instead of wrapping — perfect for image pixel data.- Core use cases: Canvas pixel manipulation, WebGL buffers, Web Audio API, binary protocol parsing, SharedArrayBuffer for multi-threading.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Typed Arrays — ArrayBuffer, Int32Array, Float64Array Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, data, structures, typed
Related JavaScript Master Course Topics