JavaScript TypedArray from() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

Uint8Array.from(source) builds a new typed array from an array, an array-like object, or any iterable such as a Set or a string. It is a static method of the hidden TypedArray parent class, so you call it on a real type like Int16Array. The optional second argument is a map function that changes each value on its way in. Its result is converted to the element type.

Remember
Uint8Array.from(source)               → new typed array
Uint8Array.from(source, mapFn)         → convert each value first
Uint8Array.from(source, mapFn, thisArg)

from: array, Set, string, { length: 3, 0: .. }
mapFn(value, index) → result is wrapped to the type
Uint8Array.from([1, 2, 3], n => n * 100)  → 100 200 44

Syntax

Call from() on a concrete typed array type. Only the source is required.

JavaScript
Uint8Array.from(source)
Uint8Array.from(source, mapFn)
Uint8Array.from(source, mapFn, thisArg)
CallWhat it does
Uint8Array.from([7, 8, 9])Copy an array into a new Uint8Array.
Int16Array.from(new Set([1, 2]))Turn any iterable, like a Set, into a typed array.
Float32Array.from(src, (v) => v * 2)Change every value while building.

Static method, available since ES2015 on all 11 typed array types.

Library declarations
JavaScript
TypedArray.from(source)
TypedArray.from(source, mapFn)
TypedArray.from(source, mapFn, thisArg)

// mapFn(value, index) -> any value, converted to the element type
// "this" must be a typed array constructor, otherwise: TypeError

Parameters

ParameterDescription
sourceAn array, array-like object (with length), or iterable. Missing items become 0 in an integer array.
mapFn (optional)A function (value, index) called for every item. Its return value is stored after conversion to the element type.
thisArg (optional)The value used as this when mapFn runs. Arrow functions ignore it.

Return Value

Return ValueDescription
TypedArrayA new typed array of the type you called it on. A BigInt array needs BigInt values, or a TypeError is thrown.

JavaScript Examples

Example 1 — Build From Different Sources

The same call accepts an array, a Set, an array-like object, and a string. Each one ends up as a Uint8Array.

JavaScript
const fromArray = Uint8Array.from([7, 8, 9]);
const fromSet = Uint8Array.from(new Set([5, 5, 6, 6, 7]));
const fromLike = Uint8Array.from({ length: 3, 0: 10, 1: 20, 2: 30 });
const fromText = Uint8Array.from('2024');

console.log(fromArray.join(' '));  // 7 8 9
console.log(fromSet.join(' '));    // 5 6 7
console.log(fromLike.join(' '));   // 10 20 30
console.log(fromText.join(' '));   // 2 0 2 4
console.log(fromText.length);      // 4
Try It Yourself

How It Works

  1. A Set removes the repeats first. The Set keeps only 5, 6, and 7, so the result has three elements.
  2. An object with length is enough. The object has length: 3 and keys 0 to 2, so from() reads those slots in order.
  3. Characters become numbers. A string is iterable, so each digit is converted to a number and "2024" turns into four elements.

Example 2 — Change Values With mapFn and thisArg

The second argument runs once per item. Its result is converted to the element type, so a Uint8Array wraps values above 255.

JavaScript
const doubled = Float32Array.from([1.5, 2.25, 4], (price) => price * 2);
console.log(doubled.join(' '));   // 3 4.5 8

const squares = Uint8Array.from({ length: 5 }, (value, index) => index * index);
console.log(squares.join(' '));   // 0 1 4 9 16

const wrapped = Uint8Array.from([1, 2, 3], (n) => n * 100);
console.log(wrapped.join(' '));   // 100 200 44

const scaled = Uint8Array.from([1, 2, 3], function (n) { return n * this.factor; }, { factor: 5 });
console.log(scaled.join(' '));    // 5 10 15
Try It Yourself

How It Works

  1. Price in, double price out. The arrow function returns twice each price, and the Float32Array stores 3, 4.5, and 8.
  2. No source array needed. { length: 5 } has no values, so only the index helps. It gives the squares 0, 1, 4, 9, and 16.
  3. Wrapping and thisArg. The result 300 does not fit in a byte and is stored as 44. The last call passes { factor: 5 } as thisArg, so this.factor is 5.

Example 3 — Conversion Rules and TypeError

Every item is converted to the element type. A missing constructor, or numbers given to a BigInt array, throw a TypeError.

JavaScript
const ints = Int16Array.from([1.9, -2.9, '7']);
console.log(ints.join(' '));               // 1 -2 7
console.log(ints instanceof Int16Array);   // true

const big = BigInt64Array.from([10n, 20n]);
console.log(String(big[1]));               // 20

const detached = Uint8Array.from;
try { detached([1, 2]); } catch (e) { console.log(e.name); }                 // TypeError
try { Uint8Array.from.call({}, [1, 2]); } catch (e) { console.log(e.name); } // TypeError
try { BigInt64Array.from([1, 2]); } catch (e) { console.log(e.name); }       // TypeError
Try It Yourself

How It Works

  1. Decimals are cut, text is parsed. The Int16Array drops the fraction of 1.9 and -2.9 (toward zero) and reads "7" as the number 7.
  2. BigInt arrays take BigInt. BigInt64Array.from([10n, 20n]) works, and String() prints the BigInt without an n.
  3. Three ways to get a TypeError. A loose copy of the function has no this, a plain object is not a constructor, and numbers cannot go into a BigInt array.

Browser & Runtime Support

TypedArray.from() is part of ES2015 (ES6), the same edition that introduced typed array methods, Set, and iterables.

✓ Baseline · ES2015

TypedArray.from()

Supported in every modern browser, plus Node.js, Deno, and Bun.

98% Modern browser support
Google Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer IE 10 and 11 have typed arrays, but not this method
Not supported
TypedArray.from() Excellent

Bottom line: Safe everywhere modern JavaScript runs. Internet Explorer 10 and 11 have typed arrays, but not the static from() method.

Frequently Asked Questions

It creates a new typed array of the type you call it on, with one element per item of an array, iterable, or array-like object.
TypedArray is only the hidden parent class. You call from() on a real type such as Uint8Array, and that type decides how values are stored.
The constructor just copies numbers. from() also accepts a map function, a thisArg, and Set or string sources.
They wrap: Uint8Array.from([300]) stores 44, and Uint8ClampedArray.from([300]) stores 255. Nothing throws.
Almost, but the result is a typed array, not a regular Array. Array.from(typedArray) goes the other way.

Next: TypedArray.of()

Build a typed array from a short list of values with the second static method.

TypedArray.of() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful