3

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?

asked Mar 26, 2017 at 5:43
2
  • It looks like it's a "prettier" form of new Array(). Commented Mar 26, 2017 at 5:59
  • A literal is just syntax, whereas Array.of is a function value. Commented Mar 26, 2017 at 12:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.