Documentación offline JavaScript main

TypedArray.of()

main Documentación oficial Licencia CC-BY-SA-2.5Descargado el 2026-08-02

En esta página

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

{{InteractiveExample("JavaScript Demo: TypedArray.of()", "shorter")}}

```js interactive-example const int16array = Int16Array.of("10", "20", "30", "40", "50");

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

## Syntax

```js-nolint
TypedArray.of()
TypedArray.of(element1)
TypedArray.of(element1, element2)
TypedArray.of(element1, element2, /* …, */ elementN)

Where TypedArray is one of:

  • {{jsxref("Int8Array")}}
  • {{jsxref("Uint8Array")}}
  • {{jsxref("Uint8ClampedArray")}}
  • {{jsxref("Int16Array")}}
  • {{jsxref("Uint16Array")}}
  • {{jsxref("Int32Array")}}
  • {{jsxref("Uint32Array")}}
  • {{jsxref("Float16Array")}}
  • {{jsxref("Float32Array")}}
  • {{jsxref("Float64Array")}}
  • {{jsxref("BigInt64Array")}}
  • {{jsxref("BigUint64Array")}}

Parameters#

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

Return value#

A new {{jsxref("TypedArray")}} instance.

Description#

See {{jsxref("Array.of()")}} for more details. There are some subtle distinctions between {{jsxref("Array.of()")}} and TypedArray.of():

  • If the this value passed to TypedArray.of() is not a constructor, TypedArray.of() will throw a {{jsxref("TypeError")}}, while Array.of() defaults to creating a new {{jsxref("Array")}}.
  • TypedArray.of() uses [[Set]] while Array.of() uses [[DefineOwnProperty]]. Hence, when working with {{jsxref("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#

{{Specifications}}

Browser compatibility#

{{Compat}}

See also#