For example,
let x = [1,2,3,5];
is equivalent to:
let x = Array.of(1,2,3,4,5);
(Unless I'm missing an important detail, which is why I'm asking the question)
You could also mix these with spread ...
syntax and variables and thus other arrays. To me, it seems Array.of()
has more overhead. Would Array.of()
have to parse an arguments object into another array?
I know there's also new Array()
as others have before questioned here, but that has a different semantic purpose, so I don't see this question as a duplicate to that.
As I see it now, Array.of()
and [ ]
seem redundant. The function's intent does seem more explicit on the former, but the latter's intent is simple enough to not be misunderstood.
So to summarize:
- When is one preferable over the other?
- Why does
Array.of()
exist when JavaScript survived without it for so long? - And, what're the differences of these two methods, if any? Would there be any needless overhead?