What is the use of the Array.from() method?

Answer

Array.from() creates a new array from an array-like or iterable object. Array-like objects have a length property and indexed elements but are not true arrays (NodeLists, HTMLCollections, arguments object, strings). Array.from(document.querySelectorAll("p")) converts a NodeList to an array (giving access to map, filter, etc.). Array.from("hello")["h","e","l","l","o"]. Array.from({length: 5}, (_, i) => i)[0,1,2,3,4] — the second argument is a map function applied to each element. Create array with specific values: Array.from({length: 3}, () => []) creates three separate empty arrays (not the same reference issue as new Array(3).fill([])). Array.from(new Set([1,2,2,3]))[1,2,3] (deduplicate). Also useful for converting Maps: Array.from(map.entries()).