docs.rodeo

MDN Web Docs mirror

NodeList

{{APIRef("DOM")}} 

NodeList objects are collections of nodes, usually returned by properties such as {{domxref("Node.childNodes")}}  and methods such as {{domxref("document.querySelectorAll()")}} .

This interface was an attempt to create an unmodifiable list and only continues to be supported to not break code that’s already using it. Modern APIs represent list structures using types based on JavaScript arrays, thus making many array methods available, and at the same time imposing additional semantics on their usage (such as making their items read-only).

These historical reasons do not mean that you as a developer should avoid NodeList. You don’t create NodeList objects yourself, but you get them from APIs such as {{domxref("Document.querySelectorAll()")}} , and these APIs are not deprecated. However, be careful of the semantic differences from a real array.

Although NodeList is not an Array, it is possible to iterate over it with forEach(). It can also be converted to a real Array using {{jsxref("Array.from()")}} .

Live vs. Static NodeLists

Although they are both considered NodeList objects, there are 2 varieties of NodeList: live and static.

Live NodeLists

In some cases, the NodeList is live, which means that changes in the DOM automatically update the collection.

For example, {{domxref("Node.childNodes")}}  is live:

const parent = document.getElementById("parent");
let childNodes = parent.childNodes;
console.log(childNodes.length); // let's assume "2"
parent.appendChild(document.createElement("div"));
console.log(childNodes.length); // outputs "3"

Static NodeLists

In other cases, the NodeList is static, where any changes in the DOM do not affect the content of the collection. The ubiquitous {{domxref("document.querySelectorAll()")}}  method returns a static NodeList.

It’s good to keep this distinction in mind when you choose how to iterate over the items in the NodeList, and whether you should cache the list’s length.

Instance properties

Instance methods

Example

It’s possible to loop over the items in a NodeList using a for loop:

for (let i = 0; i < myNodeList.length; i++) {
  let item = myNodeList[i];
}

Don’t use for...in to enumerate the items in NodeLists, since they will also enumerate its length and item properties and cause errors if your script assumes it only has to deal with {{domxref("element")}}  objects. Also, for...in is not guaranteed to visit the properties in any particular order.

for...of loops loop over NodeList objects correctly:

const list = document.querySelectorAll("input[type=checkbox]");
for (const checkbox of list) {
  checkbox.checked = true;
}

Browsers also support the iterator method ({{domxref("NodeList.forEach()", "forEach()")}} ) as well as {{domxref("NodeList.entries()", "entries()")}} , {{domxref("NodeList.values()", "values()")}} , and {{domxref("NodeList.keys()", "keys()")}} .

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

In this article

View on MDN