JavaScript Typed Arrays Explained

Learn JavaScript typed arrays with Uint8Array examples, fill() and slice() usage, numeric buffers, and how typed arrays differ from regular arrays.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

JavaScript Typed Arrays Explained

JavaScript typed arrays are array-like views for numeric binary data. They are useful when you need predictable memory layout, compact numeric storage, or data that must work well with Web APIs and buffers.

Common typed arrays include Uint8Array, Int16Array, and Float32Array. If you only need ordinary list behavior, a regular array is easier to work with, but typed arrays are better when the values must stay numeric and bounded.

Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.


Quick reference

Type Typical use
Uint8Array Bytes, files, WebGL, crypto buffers
Float32Array / Float64Array Numeric vectors, graphics, math kernels
vs Array Regular arrays allow any element type and grow flexibly

Method 1: Create a typed array

A typed array can be created from a length or from an existing list of numbers.

javascript
const bytes = new Uint8Array([1, 2, 3]);
console.log("typed-array:", bytes.join(","));
Output

You should see one line logging typed-array: 1,2,3.

You can also start with a fixed-size buffer and fill it later when data arrives from parsing, math, or a browser API.


Method 2: Use fill() and slice()

fill() writes the same value into each element, while slice() returns a copied typed array with a smaller range.

javascript
const buffer = new Uint8Array(4);
buffer.fill(7);

console.log("typed-fill:", buffer.join(","));
console.log("typed-slice:", buffer.slice(1, 3).join(","));
Output

You should see 2 lines, in order: typed-fill: 7,7,7,7, typed-slice: 7,7.

This is useful when you need to inspect part of a numeric buffer or reuse the same storage shape for repeated work.


Method 3: Compare typed arrays with regular arrays

Typed arrays are fixed to a numeric type, so they are better for bytes, channels, and calculations than free-form mixed data.

javascript
const regular = [1, 2, 3];
const typed = new Uint8Array([1, 2, 3]);

console.log("regular-array:", Array.isArray(regular));
console.log("typed-array-instance:", typed instanceof Uint8Array);
console.log("typed-array-length:", typed.length);
Output

You should see 3 lines, in order: regular-array: true, typed-array-instance: true, typed-array-length: 3.

Use a regular array for general list work and a typed array when the data is numeric, compact, or tied to binary processing.


Summary

JavaScript typed arrays give you a predictable way to handle numeric data with methods such as fill() and slice(). Use them when you need fixed numeric storage, binary data processing, or a buffer-like structure that is more specialized than a regular array.


Official documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …