The Array.of()
static method — a new addition to the JavaScript spec shipped inside ECMAScript 2015 — allows one to create an array with whatever arguments are passed it. For example,
const exampleArr = Array.of(13, 'Test', null, 17 / 2.5, true);
// => [13, "Test", null, 6.8, true]
That's all good and great, but what purposes does this method expressly serve? Much of ES6 falls under the banner of syntactic sugar, but this doesn't really seem to apply to the Array.of()
method given that we've always had array literals (i.e., []
) available to us. In other words, are there specific/unique scenarios to which the method lends itself that aren't equally well served by the ordinary array literal?
Explore related questions
See similar questions with these tags.
new Array()
.Array.of
is a function value.