A NodeList is an array-like collection of DOM nodes. It is most commonly returned by methods like querySelectorAll() and childNodes, and can contain element nodes, text nodes, or even comment nodes, depending on how it was created.
forEach(), for, or for...of.document.querySelectorAll(), node.childNodes.forEach(), for...of, and classic for loops.You usually obtain a NodeList by querying the DOM. Two very common ways are:
document.querySelectorAll(selector) – returns a static NodeList of matched elements.element.childNodes – returns a NodeList of all child nodes (elements, text, comments).A NodeList behaves similarly to an array, but is not a real array:
nodes[0], nodes[1], etc.length and item().map(), filter(), etc., unless converted.
<div>Item 1</div>
<div>Item 2</div>
<script>
const items = document.querySelectorAll("div");
console.log(items); // NodeList of div elements
console.log(items.length); // number of divs
console.log(items.item(0)); // first div element
</script>
const items = document.querySelectorAll("div");
// Using forEach (modern browsers)
items.forEach(item => {
item.style.backgroundColor = "lightblue";
});
// Using for...of
for (const item of items) {
console.log(item.innerText);
}
// Using classic for loop
for (let i = 0; i < items.length; i++) {
console.log(items[i]);
}
<ul id="list">
<li>One</li>
<li>Two</li>
</ul>
<script>
const nodeList = document.getElementById("list").childNodes;
const htmlCollection = document.getElementById("list").children;
console.log("NodeList:", nodeList); // Includes text nodes (e.g. whitespace)
console.log("HTMLCollection:", htmlCollection); // Only <li> elements
</script>
const listItems = document.querySelectorAll("li");
// Using spread syntax
const arr1 = [...listItems];
// Using Array.from()
const arr2 = Array.from(listItems);
// Now you can use array methods like map, filter, etc.
arr1.map(el => {
el.style.color = "red";
});
querySelectorAll(), childNodes, and some other APIs.forEach() supported in modern browsers.getElementsBy* methods and children.forEach() and other array methods.Below is a simple interactive example. When you click the button, it selects all list items using querySelectorAll() (NodeList) and highlights them.
This uses document.querySelectorAll("#demo-list li") under the hood.
When you run the earlier examples in a browser:
console.log(items) prints a NodeList object, showing each matched node with an index.items.length tells you how many nodes were selected.forEach(), for...of, or a classic for allows you to read or modify each node.childNodes shows extra text nodes (like whitespace), while children only shows the element nodes.map(), filter(), and reduce().querySelectorAll() for flexible and powerful CSS-style selections.map() or filter().childNodes includes whitespace and comment nodes—be careful when looping.children or getElementsBy* when you want only element nodes (HTMLCollection).forEach().childNodes of a list and detect which ones are text nodes.[...document.querySelectorAll("div")].reverse() to reverse the order of div elements and log them.childNodes.HTMLCollection to an array and use map() to get each element's text content.childNodes and log their content to the console.