Do You Know JS? Array.from In 60 Seconds

Do You Know JS? Array.from In 60 Seconds

What is array.from?

Array.from is a JavaScript method that make you create an array from any iterate-able object i.e from a String, Set, Map, NodeList basically any object that has a length property.

Examples

// Array from string
Array.from('bar');
// [ "b", "a", "r" ]

// Array from Set
const set = new Set(['foo', 'bar', 'baz', 'foo']);
Array.from(set);
// [ "foo", "bar", "baz" ]

Spoiler Alert

A method that comes with such a power has it limitation also, it is not compatible with IE Internet Explorer, but tools like Babel will translate it.

Exploring the power Array.From method brings benefit of JavaScript language to the fullest and limit the lines of codes.

Sources

Array.from()

JavaScript Array.from()