Firefox Tomorrow

javascript static method

TypedArray.of()

View on MDN ↗

The TypedArray.of() static method creates a new typed array from a variable number of arguments. This method is nearly the same as of().

Interactive exampleOpen the canonical MDN source to run this embedded demo.
const int16array = Int16Array.of("10", "20", "30", "40", "50");

console.log(int16array);
// Expected output: Int16Array [10, 20, 30, 40, 50]

Syntax

TypedArray.of()
TypedArray.of(element1)
TypedArray.of(element1, element2)
TypedArray.of(element1, element2, /* …, */ elementN)

Where TypedArray is one of:

Parameters

  • element1, …, elementN
    • : Elements used to create the typed array.

Return value

A new TypedArray instance.

Description

See of() for more details. There are some subtle distinctions between of() and TypedArray.of():

  • If the this value passed to TypedArray.of() is not a constructor, TypedArray.of() will throw a TypeError, while Array.of() defaults to creating a new Array.
  • TypedArray.of() uses [[Set]] while Array.of() uses [[DefineOwnProperty]]. Hence, when working with Proxy objects, it calls handler.set() to create new elements rather than handler.defineProperty().

Examples

Using of()

Uint8Array.of(1); // Uint8Array [ 1 ]
Int8Array.of("1", "2", "3"); // Int8Array [ 1, 2, 3 ]
Float32Array.of(1, 2, 3); // Float32Array [ 1, 2, 3 ]
Int16Array.of(undefined); // Int16Array [ 0 ]

Specifications

SpecificationsStandards references are available on the canonical MDN page.

Browser compatibility

Browser compatibilityCompatibility data is available on the canonical MDN page.

See also