It would seem that this method was introduced as a way to provide something that is messing when using the Array constructor directly. That is calling the main Array constructor method with the new keyword as a way to create a new instance of an array rather than using the bracket syntax. When doing so there is just one argument that is passed to the Array constructor if any that is used to set the starting length of the Array, not the value of the first element. This can cause some confusion with new developers that are and familiar with this. So the Array of method is now that are yet another way to create a new array by passing some arguments for the starting element values for the array.
I can not say that I use the Array of method often, as I prefer to use some of the older tired yet true ways of doing the same thing that is just a little more involved. But never the less this post will be on the JS array of meto9d and other ways of creating a new array with a set number of starting values.
Today though there are a few other options that can be used to create a new Array, one of which is the Array of method. This is a static method of the js built in Array object that does not need to be used with the new keyword. Just call the of method of the Array object and pass values for the new array by way of function call arguments.
So another Static Array method of interest here now it the Array from method. Just like the Array of method it can be used to create a new Array, but it does so by way of creating an array from a source object. When it comes to source Objects many such Objects might be Array like Objects. That is that they are just Plain old objects, or Objects that where crated with a constructor other then that of the Array constructor. As such they do not have Array prototype methods because they are not an instance of the Array constructor. So the Array from method is one way to create a new array by way of passing one of these objects as the first argument.
Ask yourself this when creating a new array \”What is the goal here?\”. Often the goal with all of this is to just simple create a new array, rather than mutating an array that all ready exists. In that situation using a method like Array of, or the bracket syntax will do just that. However there are many methods in the Array prototype that will return a new Array rather than mutating an array in place. One such method is the Array map method.