Array.from() vs spread syntax Array.from() vs spread syntax arrays arrays

Array.from() vs spread syntax


Spread element (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 (i.e. objects that have the length property and indexed elements) which are not iterable. 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 functionconsole.log([...arrayLikeObject]);