DEV Community

Discussion on: Three dots ( … ) in JavaScript

Collapse
 
wheatup profile image
Hao • Edited

The other usage of ... I found is very useful:

Converting an iterable object:

// An array of HTMLElement, not the annoying NodeList object
const array = [...document.querySelectorAll('div')]; 
Enter fullscreen mode Exit fullscreen mode

Even a generator, how cool is that?

const array = [...(function*() {
  for (let i = 10; i > 0; i--) {
    yield i;
  }
  yield 'Launch';
})()];

console.log(array);    // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, "Launch"]
Enter fullscreen mode Exit fullscreen mode