Array.of
Creates an array from its arguments.
Available since: ES2015
Syntax
Array.of(element1, element2, /* … */)Parameters
| Parameter | Type | Description |
|---|---|---|
| element1, … | any | The elements of the new array. |
Return value
ArrayA new array of the given elements.
Examples
The difference between Array.of and new Array with one number:
console.log(Array.of(3).length);
console.log(new Array(3).length);Output:
1
3Notes
new Array(3) creates an empty array of length 3, while Array.of(3) creates an array with one element, 3. Usually the simplest is the literal [3].
See also
- Array.from — Creates an array from a string, Set, Map or another iterable.
- Array.prototype.length — The number of elements in an array.
In the tutorial
- Arrays — What an array is in JavaScript: creating one, indexes, length, changing elements and nested arrays.