Array.of()

A fix for one specific wart. The Array constructor treats a single numeric argument as a LENGTH rather than a value, and Array.of never does.

Array static methodES2015Live demo
Common call
Array.of(3)
Returns
[3] — always the values, never a length
Replaces
the Array constructor when the count of arguments is dynamic
Watch out
rarely needed in practice; [x] is shorter and clearer
Array.of(...values...values — Any number of values, each becoming one element. With no arguments you get an empty array.type: any · default: none)
→ Array

Demo

Live evaluation
Try:
Inputs
valuenumbera single number
Output
Array.of(3)
[3]

Every case gives a ONE-element array holding the number — Array.of(3) is [3]. Compare that with the Array constructor: Array(3) produces an array of LENGTH three containing nothing at all, three empty slots. Array.of removes that special case entirely, so a single numeric argument behaves like any other value.

Parameters

NameTypeRequiredDescription
...valuesanyno (none)Any number of values, each becoming one element. With no arguments you get an empty array.

Return value

Array — A new array containing exactly the arguments given — including when there is only one, and it is a number.

Common patterns

Build from a dynamic argument list
Where a single numeric value would break the constructor.
const wrapped = Array.of(...values);
Prefer a literal when you can
For a known value, brackets are shorter and just as correct.
const one = [value];
The constructor trap it avoids
Worth knowing even if you never call Array.of.
Array(3);      // three empty slots
Array.of(3);   // [3]

Examples

1. A single number
Array.of(3)
Returns
[3]
2. The constructor differs
Array(3)
Returns
[ <3 empty items> ]
3. And its length
Array(3).length
Returns
3
4. Several values
Array.of(1, 2, 3)
Returns
[1, 2, 3]
5. Constructor agrees here
Array(1, 2, 3)
Returns
[1, 2, 3]
6. A literal is simpler
[3]
Returns
[3]

Pitfalls

1. It is rarely the right tool
An array literal does the same job in fewer characters and reads better. Array.of only earns its place when the arguments are spread from something dynamic and a single numeric value would otherwise hit the constructor trap.
Roundabout
const a = Array.of(value);
[value]
Just a literal
const a = [value];
[value]
2. The trap it fixes belongs to Array(), not Array.of
The surprising behaviour is the CONSTRUCTOR treating one number as a length. Array.of is the well-behaved version — if you never call Array(n) expecting a value, you will never miss it.
Constructor
Array(3)
[ <3 empty items> ] — length 3, no values
of, or a literal
Array.of(3)
[3]
3. It is a static, not an instance method
Array.of(...), never x.of(...). The same slip as with Array.from.
Not on instances
[1].of(2)
TypeError: [1].of is not a function
Call on Array
Array.of(2)
[2]

When to use

Use it
  • Spreading a dynamic argument list where one numeric value is possible
  • Writing generic code over array-like constructors
Reach for something else
  • You know the values → an array literal is shorter and clearer
  • You want an array of a given LENGTH → new Array(n).fill(...)
  • You are converting an iterable → Array.from

Notes

Complexity
O(n) in the number of arguments
Return
A new dense array; no holes are ever created
CPython impl
V8: Builtins-array-of.tq
Memory
Allocates one array sized to the argument count
Thread-safe
Single-threaded

FAQ

Because the Array constructor has a special case: exactly one numeric argument is read as a LENGTH, producing that many empty slots. With any other count, or any non-number, the arguments become the elements. Array.of removes the special case.

Array(3);        // [ <3 empty items> ]
Array(3, 4);     // [3, 4]
Array.of(3);     // [3]

History

ES2015
Array.of added alongside Array.from, specifically to sidestep the single-numeric-argument constructor behaviour.