Discover two innovative methods introduced in ES6
for Array
: Array.of()
and Array.from()
. Their distinct uses are outlined on this informative page.
However, I find myself perplexed by the application of Array.of
in this specific line.
// usage of Array.of
console.log(
Array.of( "things", "that", "aren't", "currently", "an", "array" )
); // ["things", "that", "aren't", "currently", "an", "array"]
Consider the alternative approach below:
console.log(
[ "things", "that", "aren't", "currently", "an", "array" ]
); // ["things", "that", "aren't", "currently", "an", "array"]
It's worth noting that the same outcome can be achieved using console.log(Array.of(...))
. Is there any particular benefit to utilizing Array.of
in this context?
Another source of confusion arises with the use of Array.from
in the following line.
var divs = document.querySelectorAll("div");
console.log(
Array.from( divs )
);
What if Array.from
is omitted from the aforementioned code snippets.
var arr = [1, 2, 3];
console.log(Array.from(arr)); // [1, 2, 3]
console.log(arr); // [1, 2, 3]
Are there any advantages to employing Array.from
in this scenario?