#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

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:

nodeTypeKindnodeName example
1Element"LI"
3Text"#text"
8Comment"#comment"
9Document"#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.
  • childNodes is a live NodeList, and children, getElementsByClassName() and getElementsByTagName() return a live HTMLCollection. 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() and prepend() add nodes or text at the end or start of a parent.
  • before(), after() and replaceWith() 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.

Explore Course