Today, we will explore different ways to convert a NodeList into an Array.
Array.from() method creates a new Array instance from an iterable object. In our case, the iterable is the NodeList.
This method is part of the ES6 specification and is widely supported in modern browsers.
The spread operator (
...) allows iterables such as NodeList to be expanded into places where zero or more arguments or elements are expected.
This is also an ES6 feature.
Before ES6, the common way to convert a NodeList to an array was by using
This method works in all browsers including IE6.
To loop through a NodeList, you can use the
forEach method after converting it to an array.
Join our community of forward-thinkers and innovators. Subscribe to get the latest updates on courses, exclusive insights, and tips from industry experts directly to your inbox.