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
Name
Type
Required
Description
...values
any
no (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.
constwrapped = Array.of(...values);
Prefer a literal when you can
For a known value, brackets are shorter and just as correct.
constone = [value];
The constructor trap it avoids
Worth knowing even if you never call Array.of.
Array(3); // threeemptyslotsArray.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
consta = Array.of(value);
[value]
Just a literal
consta = [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(...)
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.