Web/JavaScript/Reference/Global objects/TypedArray/of
The TypedArray.of() method creates a new typed array from a variable number of arguments. This method is nearly the same as Array.of().
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
TypedArray.of(element0[, element1[, ...[, elementN]]])
Where TypedArray is one of:
Int8ArrayUint8ArrayUint8ClampedArrayInt16ArrayUint16ArrayInt32ArrayUint32ArrayFloat32ArrayFloat64ArrayBigInt64ArrayBigUint64Array
Parameters
elementN- Elements of which to create the typed array.
Return value
A new TypedArray instance.
Description
Some subtle distinctions between Array.of() and TypedArray.of():
- If the
thisvalue passed toTypedArray.of()is not a constructor,TypedArray.of()will throw aTypeError, whereArray.of()defaults to creating a newArray. TypedArray.of()usesWeb/JavaScript/Reference/Global objects/TypedArray/PutwhereArray.of()usesWeb/JavaScript/Reference/Global objects/TypedArray/DefineProperty. Hence, when working withProxyobjects, it callshandler.setto 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
| Specification |
|---|
| [(ECMA-262)The definition of '%TypedArray%.of' in that specification.] |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
of
|
Chrome
Full support 45 |
Edge
Full support 14 |
Firefox
Full support 38 |
IE
No support No |
Opera
No support No |
Safari
Full support 9.1 |
WebView Android
No support No |
Chrome Android
No support No |
Firefox Android
Full support 38 |
Opera Android
No support No |
Safari iOS
Full support 9.3 |
Samsung Internet Android
No support No |
nodejs
Full support 4.0.0 |
Legend
- Full support
- Full support
- No support
- No support
See also
TypedArray.of() by Mozilla Contributors is licensed under CC-BY-SA 2.5.