Spread element or syntax (note that it’s not an operator) works only with objects that are iterable (i.e. implement the @@iterator
method). Array.from()
works also on array-like objects which are not iterable (i.e. objects that have indexed elements and a length
property).
See this example:
const arrayLikeObject = { 0: 'a', 1: 'b', length: 2 };
// This logs ['a', 'b']
console.log(Array.from(arrayLikeObject));
// This throws TypeError: arrayLikeObject[Symbol.iterator] is not a function
console.log([...arrayLikeObject]);
Also, if you just want to convert something to an array, I think it’s better to use Array.from()
because it’s more readable. Spread elements are useful for example when you want to concatenate multiple arrays (['a', 'b', ...someArray, ...someOtherArray]
).