JavaScript Typed Arrays

JavaScript Tutorial

JavaScript Typed Arrays

JavaScript Typed Arrays provide a way to handle binary data efficiently. Unlike normal arrays, typed arrays store fixed-size, homogeneous data (all elements of the same type) and are commonly used in graphics, WebGL, and performance-critical tasks.

 Why Typed Arrays?

  • Regular arrays can hold any type (numbers, strings, objects), which is flexible but slower.

  • Typed arrays are faster because they store data in contiguous memory with a fixed type.

 Types of Typed Arrays

Typed ArrayDescription
Int8Array8-bit signed integers
Uint8Array8-bit unsigned integers
Uint8ClampedArray8-bit unsigned integers, clamped to 0–255
Int16Array16-bit signed integers
Uint16Array16-bit unsigned integers
Int32Array32-bit signed integers
Uint32Array32-bit unsigned integers
Float32Array32-bit floating-point numbers
Float64Array64-bit floating-point numbers

 Creating Typed Arrays

From a length:

From an array:

 Accessing and Modifying Elements


 

 Typed Array Properties

PropertyDescription
lengthNumber of elements
bufferArrayBuffer representing the raw binary data
BYTES_PER_ELEMENTSize in bytes of each element

 Iterating Typed Arrays


 

 Converting Typed Array to Regular Array

 Practical Uses

  • Graphics and WebGL programming

  • Handling binary files

  • Efficient numeric computations

  • Audio and video processing

Summary Table

FeatureDescription
Fixed sizeLength cannot be changed after creation
HomogeneousAll elements same type
FastStored in contiguous memory
Common typesInt8Array, Uint8Array, Float32Array, etc.
Use casesGraphics, binary data, performance-critical tasks

You may also like...