Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

NodeList — a collection of DOM nodes

NodeList is an indexed, iterable collection of DOM nodes. It can be live or static, depending on the API that returns it.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

list.length; list[index]; list.item(index); for (const node of list) { /* ... */ }

NodeList is a DOM interface, not a JavaScript array. Its nodes are in tree order by default, unless the API defining the collection specifies a different order. length gives the number of nodes. Indices start at zero. The argument to item(index) is first converted to an unsigned 32-bit integer (Web IDL's unsigned long type). The method returns null if the converted index is out of range. A list[index] lookup uses a property key and does not perform that conversion. On a nonempty list, list.item(2 ** 32) therefore returns the first node (index 0), while list[2 ** 32] normally returns undefined.

childNodes returns a live collection that updates after DOM changes and also includes text nodes. querySelectorAll() returns a static collection of matching elements. Static refers to collection membership, not to the nodes themselves being immutable.

Use for...of to traverse the collection. Changing the DOM while iterating a live collection can change its contents and indices; when you need fixed membership, first create an array using Array.from(list).

Examples

const root = document.createElement('div');
root.append(document.createElement('span'));

const live = root.childNodes;
const snapshot = root.querySelectorAll('span');
root.append(document.createElement('span'));

console.log(live.length, snapshot.length);
console.log(snapshot.item(9) === null);

let count = 0;
for (const node of live) count++;
console.log(count);

Result

2 1
true
2

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources