TypedArray.of()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It's been available across browsers since September 2016.
The TypedArray.of() static method creates a new typed array from a variable number of arguments. This method is nearly the same as Array.of().
Try it
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:
Int8ArrayUint8ArrayUint8ClampedArrayInt16ArrayUint16ArrayInt32ArrayUint32ArrayFloat16ArrayFloat32ArrayFloat64ArrayBigInt64ArrayBigUint64Array
Parameters
-
element1, …,elementN - Elements used to create the typed array.
Return value
A new TypedArray instance.
Description
See Array.of() for more details. There are some subtle distinctions between Array.of() and TypedArray.of():
- If the
thisvalue passed toTypedArray.of()is not a constructor,TypedArray.of()will throw aTypeError, whileArray.of()defaults to creating a newArray. -
TypedArray.of()uses[[Set]]whileArray.of()uses[[DefineOwnProperty]]. Hence, when working withProxyobjects, it callshandler.set()to create new elements rather thanhandler.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
Browser compatibility
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on iOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
of |
45 |
12 |
38 |
32 |
10 |
45 |
38 |
32 |
10 |
5.0 |
45 |
10 |
1.0.0 |
1.0 |
4.0.0 |
See also
- Polyfill of
TypedArray.ofincore-js - JavaScript typed arrays guide
TypedArrayTypedArray.from()Array.of()
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/of