Node and Node-list handling
In the DOM, a node is any single item in the page tree: an element, a piece of text, a comment or the document itself. A NodeList is a list-like collection of nodes, returned by methods such as querySelectorAll and properties such as childNodes. Handling them well means knowing which kind of node you have, how to loop over collections, and whether a collection updates by itself when the page changes.
Nodes are more than elements
Every element is a node, but not every node is an element. The whitespace and line breaks between tags in your HTML become text nodes. Each node has a nodeType number and a nodeName:
| nodeType | Kind | nodeName example |
|---|---|---|
| 1 | Element | "LI" |
| 3 | Text | "#text" |
| 8 | Comment | "#comment" |
| 9 | Document | "#document" |
That is why there are two families of navigation properties. childNodes, firstChild and nextSibling include text nodes. children, firstElementChild and nextElementSibling skip them and return elements only, which is usually what you want.
Static and live collections
This difference catches many developers:
querySelectorAll()returns a static NodeList: a snapshot that does not change when the page does.childNodesis a live NodeList, andchildren,getElementsByClassName()andgetElementsByTagName()return a liveHTMLCollection. They update automatically as elements are added or removed.
<ul id="tasks">
<li class="done">Buy milk</li>
<li class="done">Pay bill</li>
<li>Call Ravi</li>
</ul>
<script>
const list = document.getElementById('tasks');
console.log(list.childNodes.length); // 7: includes whitespace text nodes
console.log(list.children.length); // 3: elements only
const snapshot = list.querySelectorAll('li'); // static
const live = list.getElementsByTagName('li'); // live
const extra = document.createElement('li');
extra.textContent = 'Water plants';
list.append(extra);
console.log(snapshot.length); // still 3
console.log(live.length); // 4
// NodeList supports forEach directly
snapshot.forEach((li, i) => { li.dataset.index = i; });
// HTMLCollection does not: convert it first
Array.from(live)
.filter((li) => li.classList.contains('done'))
.forEach((li) => li.remove());
</script>
Looping and converting
A NodeList is not an array. It has length, index access, forEach and works with for...of, but lacks map, filter and reduce. An HTMLCollection does not even have forEach. When you need array methods, convert with Array.from(collection) or the spread syntax [...collection]. Array.from also accepts a mapping function as its second argument, so Array.from(snapshot, (li) => li.textContent) builds an array of task names in one step.
The live-collection removal bug
Removing items while looping forward over a live collection skips elements. Suppose you loop with for (let i = 0; i < live.length; i++) and remove each matching item. When item 0 is removed, the old item 1 shifts into position 0, but the loop moves on to index 1, so it is never checked. Avoid this by converting to an array first, as in the example, by looping backwards from the end, or by using querySelectorAll, whose static list does not shift.
Useful node methods
append()andprepend()add nodes or text at the end or start of a parent.before(),after()andreplaceWith()act relative to the node itself.remove()deletes the node from the page.cloneNode(true)copies a node and all its descendants.contains(other)checks whether a node is inside another.
Key takeaways
Text and comment nodes sit between elements, so prefer the element-only properties like children. querySelectorAll gives a static snapshot; getElementsBy... and childNodes stay live. Convert collections with Array.from before using array methods or removing items in a loop.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.