@Gabrielse's post answers the question pretty well. However, there's just one more thing that I'd like to add to it.
One use-case where you might do something like this is when you have a set
iterable and you want its elements inside an array. for e.g.
const set = new Set();
set.add('foo');
set.add('bar');
set.add('baz');
Suppose, you've the items foo
, bar
, baz
in a set. A simple console.log(set);
would result Set { 'foo', 'bar', 'baz' }
. The result you get of course is a set. But what if you want these items in an array?
A simple way to do this would be to use ...
(spread operator) applied to the set
inside an array. The result of console.log([...set]);
would be [ 'foo', 'bar', 'baz' ]
.