JavaScript TypedArray of() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

Int16Array.of(10, -20, 30) creates a new typed array whose elements are exactly the arguments you pass. It is a static method of the hidden TypedArray parent class, so you call it on a real type such as Int16Array or Uint8Array. Every argument is converted to the element type, so out-of-range numbers wrap around. The big difference from the constructor: Uint8Array.of(3) makes one element with the value 3, but new Uint8Array(3) makes three zeros.

Remember
Int16Array.of(10, -20, 30)  → 10 -20 30

Uint8Array.of(3)     → [3]       (one element, value 3)
new Uint8Array(3)    → [0, 0, 0] (three elements)

Uint8Array.of(256, -1)  → 0 255  (wraps)
Uint8Array.of()         → empty

Syntax

Pass the elements as separate arguments. Use the spread syntax to pass an array.

JavaScript
Uint8Array.of()
Uint8Array.of(element0)
Uint8Array.of(element0, element1, /* ..., */ elementN)
CallWhat it does
Int16Array.of(10, -20, 30)Build an Int16Array with three elements.
Uint8Array.of(...list)Spread an array into the arguments.
Float32Array.of()No arguments gives an empty typed array of length 0.

Static method, available since ES2015 on all 11 typed array types. The type you call it on decides how every argument is stored.

Library declarations
JavaScript
TypedArray.of(...items)

// each item is converted to the element type
// "this" must be a typed array constructor, otherwise: TypeError

Parameters

ParameterDescription
element0, ..., elementNZero or more values. Each one is converted to the element type, and the number of arguments becomes the length.

Return Value

Return ValueDescription
TypedArrayA new typed array of the type you called it on. BigInt arrays need BigInt arguments, or a TypeError is thrown.

JavaScript Examples

Example 1 — Sensor Readings in an Int16Array

Temperatures can go below zero, so an Int16Array is a good home. We list the readings as arguments.

JavaScript
const readings = Int16Array.of(120, -35, 0, 980);

console.log(readings.join(', '));              // 120, -35, 0, 980
console.log(readings.length);                  // 4
console.log(readings instanceof Int16Array);   // true
console.log(readings.byteLength);              // 8
Try It Yourself

How It Works

  1. One argument, one element. The four arguments become four elements, in the same order you typed them.
  2. Negative numbers are fine. An Int16Array is signed, so -35 is stored as it is and printed with its minus sign.
  3. Two bytes each. Four Int16 elements need 8 bytes, which byteLength confirms.

Example 2 — Out-of-Range Values Wrap or Clamp

Arguments are converted to the element type. A byte array wraps, a clamped array stops at 0 and 255, and an Int8Array wraps into negatives.

JavaScript
const wrapped = Uint8Array.of(255, 256, -1, 300);
console.log(wrapped.join(' '));   // 255 0 255 44

const clamped = Uint8ClampedArray.of(255, 256, -1, 300);
console.log(clamped.join(' '));   // 255 255 0 255

const signed = Int8Array.of(127, 128);
console.log(signed.join(' '));    // 127 -128
Try It Yourself

How It Works

  1. Only 8 bits are kept. In the Uint8Array, 256 is stored as 0, -1 as 255, and 300 as 44, because the value is taken modulo 256.
  2. Clamping stops at the edges. The Uint8ClampedArray turns 256 and 300 into 255 and -1 into 0 instead of wrapping.
  3. Signed bytes go negative. An Int8Array tops out at 127, so 128 wraps to -128.

Example 3 — of() Versus new Uint8Array()

The same single number means two different things. We also use of() with BigInt values and show the TypeError for plain numbers.

JavaScript
const sized = new Uint8Array(3);
const single = Uint8Array.of(3);
console.log(sized.join(' ') + ' | length ' + sized.length);    // 0 0 0 | length 3
console.log(single.join(' ') + ' | length ' + single.length);  // 3 | length 1

const spread = Uint8Array.of(...[4, 5, 6]);
console.log(spread.join(' '));                                  // 4 5 6
console.log(Uint8Array.of().length);                            // 0

const ids = BigInt64Array.of(1n, 2n, 3n);
console.log(String(ids[2]));                                    // 3
try { BigInt64Array.of(1); } catch (e) { console.log(e.name); } // TypeError
Try It Yourself

How It Works

  1. The constructor sees a size. new Uint8Array(3) reserves three bytes that start as zero.
  2. of() sees a value. Uint8Array.of(3) stores the number 3 as the only element, so its length is 1.
  3. Spread, empty, and BigInt. Spread passes an array as separate arguments, no arguments gives length 0, and a BigInt array accepts only BigInt arguments.

Browser & Runtime Support

TypedArray.of() is part of ES2015 (ES6), the same edition that added the static from() method and the typed array methods.

✓ Baseline · ES2015

TypedArray.of()

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.of() Excellent

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

Frequently Asked Questions

It creates a new typed array of the type you call it on, using its arguments as the elements. Int16Array.of(1, 2, 3) returns an Int16Array with three elements.
Because of() always treats arguments as values. Only the constructor treats a single number as the length. Use new Uint8Array(3) for three zeros.
Use the spread syntax: Uint8Array.of(...list). If you already have an array, Uint8Array.from(list) or new Uint8Array(list) is simpler.
They wrap to fit the type. Uint8Array.of(256) is 0 and Int8Array.of(128) is -128. A Uint8ClampedArray clamps to 0 and 255 instead.
Yes, but every argument must be a BigInt such as 1n. Passing a normal number throws a TypeError.

Next: at()

Read one element by index, including negative positions from the end.

at() 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