Array.of()
Creates a new array from its arguments, regardless of how many there are or what type they have.
Array.of()
Array.of(element1)
Array.of(element1, element2)
Array.of(element1, element2, /* …, */ elementN)- Added in JavaScript ES2015
Parameters
| Parameter | Type | Description |
|---|---|---|
$element1, …, elementNoptional | any | Elements used to create the array. Any number of arguments of any type; each becomes one element. |
Return value
ArrayA new Array instance containing the arguments, in order.
Changelog
- PHP ES2015Added to give the Array constructor a single-argument-safe counterpart, mainly for typed arrays and Array subclasses.
Array.of() is the fix for the one genuinely broken corner of the Array constructor. Array(1, 2, 3) gives [1, 2, 3], but Array(7) gives an array of seven empty slots with length 7 — the single-number case is special-cased into a capacity hint. Array.of() has no special case: every argument becomes exactly one element, so Array.of(7) is [7].
In everyday code you rarely need it, because an array literal [7] is shorter and does the same thing. It earns its place when the constructor is not written literally:
- You have a constructor in a variable and cannot know the argument count at author time.
- You are working with a subclass of
Array.Array.of()is a generic factory: it usesthisas the constructor, soMyList.of(1, 2)returns aMyList, not a plain array. - You are writing code that mirrors typed arrays, where
Int8Array.of()andInt8Array(n)differ the same way.
If this is not a constructor, it falls back to plain Array, which is why detached use like const of = Array.of still works.
Example
When to use it
- Build a one-element array when the value might be a number
- Write an Array subclass factory that returns instances of the subclass
- Create an array from a spread of unknown arity without hitting the Array(n) special case
Frequently asked questions
What is the difference between Array.of() and Array()?
They differ only when given exactly one number: Array.of(7) creates [7], while Array(7) creates an empty array with length 7. For zero arguments or two or more arguments of any type, the two are identical.
Should I use Array.of() instead of an array literal?
No, prefer the literal for fixed content — [7] is shorter, clearer and at least as fast. Array.of() is for the cases a literal cannot express: subclass factories, and code where the constructor or the arguments come from variables.
Does Array.of() work on Array subclasses and typed arrays?
Yes, it is a generic factory that constructs using its this value, so Stack.of(1, 2) returns a Stack. Typed arrays have their own equivalent, such as Int8Array.of(), which behaves the same way relative to Int8Array(n).
More examples
Holes are not undefined
Both arrays have length 3, but forEach visits zero elements in the hole-filled one and three in the other. Empty slots are skipped by forEach, map, filter and reduce, which is why an Array(n) placeholder often looks inert until you .fill() it.
const holes = Array(3);
const real = Array.of(undefined, undefined, undefined);
console.log(holes.length, real.length);
let visited = 0;
holes.forEach(() => visited++);
console.log("forEach visited in holes:", visited);
visited = 0;
real.forEach(() => visited++);
console.log("forEach visited in real:", visited);Generic factory on a subclass
Stack inherits of() as a static method, and because of() constructs using this, the result is a Stack with its own methods — while still being a true array. The detached call shows the fallback: with no constructor as this, plain Array is used instead of throwing.
class Stack extends Array {
peek() {
return this[this.length - 1];
}
}
const s = Stack.of(1, 2, 3);
console.log(s instanceof Stack, s.peek());
console.log(Array.isArray(s));
// this is the constructor, so a plain call falls back to Array
const detached = Array.of;
console.log(detached("a", "b") instanceof Array);When the argument count is unknown
wrap(5) returns [5] in every case. The constructor version returns an array of length 5 for the same input, because spreading a single number reaches the Array(n) special case — the exact bug Array.of() was added to remove.
function wrap(...values) {
return Array.of(...values);
}
console.log(wrap(5));
console.log(wrap(5, 6));
console.log(wrap());
// the same code written with the constructor is a trap
function wrapBad(...values) {
return Array(...values);
}
console.log(wrapBad(5).length, wrap(5).length);
Discussion