In Javascript, what is the best way to convert a NodeList to an array

cc young picture cc young · Sep 18, 2011 · Viewed 36.9k times · Source

The DOM method document.querySelectorAll() (and a few others) return a NodeList.

To operate on the list, e.g. using forEach(), the NodeList must first be converted to an Array.

What's the best way to convert the NodeList to an Array?

Answer

Freezystem picture Freezystem · Jun 8, 2017

With ES6 you can simply do:

const spanList = [...document.querySelectorAll("span")];