JavaScript Array.of() Method
The Array.of() static method creates a new array from its arguments, avoiding the single-number quirk of the Array() constructor.
Description
The Array.of() static method creates an array from whatever arguments you pass. It exists because of a quirk in the Array() constructor: Array(3) creates an empty array of length 3, but Array.of(3) creates [3]. In everyday code, an array literal is simpler.
It exists mainly for completeness and for subclasses of Array: MyArray.of(1, 2) creates an instance of the subclass, which a literal can't do. It's also occasionally useful as a function reference, for example passing Array.of where a function that collects its arguments into an array is expected.
Every argument becomes one element, whatever its type, so Array.of(undefined) is an array with one undefined element and Array.of([1, 2]) is an array containing one nested array. To build an array from an existing iterable or array-like object, use Array.from() instead.
Syntax
Array.of(element1, element2, ...)Parameters
- element1, element2, ...The elements of the new array.
Return Value
A new array.
Example
Browser Support
The following information will show you the current browser support for the Array.of() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





