Skip to main content
Menu

How to convert a NodeList to an array

<!-- Example HTML markup -->
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
const items = document.querySelectorAll('li');
console.log(items) // NodeList(4) [li, li, li, li]

[...]

const items = [...document.querySelectorAll('li')];
console.log(items) // (4) [li, li, li, li]

MDN - Spread syntax

Array.from()

const itemsArray = Array.from(document.querySelectorAll('li'));
console.log(items) // (4) [li, li, li, li]

MDN - Array.from()

[].slice.call

const items = [].slice.call(document.querySelectorAll('li'));
console.log(items) // (4) [li, li, li, li]

MDN - Array.prototype.slice()