NodeList

Query results and child lists are a single static NodeList type. There is no separate HTMLCollectionchildren also returns a NodeList. It is a snapshot taken when produced: later mutations to the tree are not reflected.

A NodeList backs childNodes, children, querySelectorAll, and getElementsByTagNameNS.

Members

Member Behaviour
length The number of nodes.
item(index) The node at index, or null (negative or out-of-range → null).

It is iterablefor…of, spread, and Array.from all work.

const items = document.querySelectorAll("li");

items.length;        // e.g. 3
items.item(0);       // first <li>, or null
items.item(-1);      // null (no negative indexing)

for (const li of items) {
  li.classList.add("counted");
}

const titles = Array.from(items, li => li.textContent);

Because a NodeList is a snapshot, removing a node during iteration does not shorten the list — the snapshot still holds every node that matched when it was produced.

See also