JavaScript Typed Array Reference
Complete reference guide for all typed array types and properties
📚 Typed Array Reference Guide
This comprehensive reference covers all typed array types, their properties, and use cases. Use this as your go-to guide for typed array development.
// Quick reference example
const int8 = new Int8Array(4); // 8-bit signed integers
const uint8 = new Uint8Array(4); // 8-bit unsigned integers
const float32 = new Float32Array(4); // 32-bit floating point
console.log(int8.BYTES_PER_ELEMENT); // 1
Output:
1
All Typed Array Types
Int8Array
8-bit signed integers (-128 to 127)
new Int8Array(4)
// 1 byte per element
Uint8Array
8-bit unsigned integers (0 to 255)
new Uint8Array(4)
// 1 byte per element
Int16Array
16-bit signed integers (-32,768 to 32,767)
new Int16Array(4)
// 2 bytes per element
Float32Array
32-bit floating point numbers
new Float32Array(4)
// 4 bytes per element
🔹 Complete Type Reference
All available typed array types with their specifications:
// 8-bit types
const int8 = new Int8Array([127, -128]); // -128 to 127
const uint8 = new Uint8Array([255, 0]); // 0 to 255
const uint8Clamped = new Uint8ClampedArray([300, -10]); // Clamped 0-255
// 16-bit types
const int16 = new Int16Array([32767, -32768]); // -32,768 to 32,767
const uint16 = new Uint16Array([65535, 0]); // 0 to 65,535
// 32-bit types
const int32 = new Int32Array([2147483647]); // -2^31 to 2^31-1
const uint32 = new Uint32Array([4294967295]); // 0 to 2^32-1
// Floating point types
const float32 = new Float32Array([3.14159]); // 32-bit float
const float64 = new Float64Array([Math.PI]); // 64-bit float
// BigInt types (64-bit)
const bigInt64 = new BigInt64Array([9007199254740991n]);
const bigUint64 = new BigUint64Array([18446744073709551615n]);
Memory Usage:
Int8Array: 1 byte per element
Int16Array: 2 bytes per element
Int32Array: 4 bytes per element
Float64Array: 8 bytes per element
🔹 Properties Reference
Essential properties available on all typed arrays:
const arr = new Float32Array([1.5, 2.7, 3.9]);
// Static properties
console.log(Float32Array.BYTES_PER_ELEMENT); // 4
console.log(Float32Array.name); // "Float32Array"
// Instance properties
console.log(arr.length); // 3 (number of elements)
console.log(arr.byteLength); // 12 (total bytes: 3 * 4)
console.log(arr.byteOffset); // 0 (offset in buffer)
console.log(arr.buffer); // ArrayBuffer object
// Constructor reference
console.log(arr.constructor === Float32Array); // true
Output:
4
"Float32Array"
3
12
0
true
🔹 Method Categories
Typed arrays inherit methods from Array.prototype:
const numbers = new Int16Array([10, 20, 30, 40, 50]);
// Accessor methods (don't modify original)
console.log(numbers.slice(1, 3)); // Int16Array [20, 30]
console.log(numbers.indexOf(30)); // 2
console.log(numbers.includes(40)); // true
// Iteration methods
const doubled = numbers.map(x => x * 2);
const evens = numbers.filter(x => x % 20 === 0);
const sum = numbers.reduce((a, b) => a + b, 0);
console.log(doubled); // Int16Array [20, 40, 60, 80, 100]
console.log(evens); // Int16Array [20, 40]
console.log(sum); // 150
Output:
Int16Array [20, 30]
2
true
Int16Array [20, 40, 60, 80, 100]
Int16Array [20, 40]
150
🔹 Use Case Guide
Choose the right typed array for your needs:
// Image processing (RGB values 0-255)
const imageData = new Uint8Array(width * height * 3);
// Audio processing (floating point samples)
const audioBuffer = new Float32Array(sampleRate * duration);
// Game coordinates (signed integers)
const positions = new Int16Array([-100, 50, 200, -75]);
// Scientific calculations (high precision)
const measurements = new Float64Array([3.141592653589793]);
// File I/O (byte data)
const fileBytes = new Uint8Array(fileSize);
// Memory-efficient counters (small positive numbers)
const counters = new Uint16Array(1000); // 0 to 65,535
console.log("Image pixel:", imageData[0]);
console.log("Audio sample:", audioBuffer[0]);
console.log("Game position:", positions[0]);
Best Practices:
• Use Uint8Array for binary data and images
• Use Float32Array for graphics and audio
• Use Int32Array for general numeric data
• Use Float64Array for scientific precision