JavaScript Node childNodes Property

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Live NodeList

What You’ll Learn

The Node.childNodes property returns a live NodeList of a node’s direct children (elements, text, and comments). Learn whitespace gotchas, childNodes vs children, safe loops, and clearing children—with five examples and try-it labs.

01

Kind

Read-only property

02

Returns

Live NodeList

03

Includes

Elements + text

04

vs children

Elements only

05

Gotcha

Whitespace nodes

06

Status

Baseline widely

Introduction

The DOM is a tree. Each node can have child nodes hanging underneath it. childNodes is how you read that direct-child list from JavaScript.

Unlike a plain array snapshot, the list is live: add or remove a child and the NodeList updates. Items are node objects (use .nodeName, .nodeType, .textContent), not strings.

💡
Beginner tip

Pretty-printed HTML creates whitespace text nodes. Prefer element.children or firstElementChild when you only care about element tags.

Understanding childNodes

MDN: the read-only childNodes property returns a live NodeList of child nodes of the given node. The first child is at index 0. Child nodes include elements, text, and comments.

  • Live list — length and indexes change as the DOM changes.
  • Same object — several calls to childNodes return the same NodeList.
  • Whitespace — newlines/indentation in HTML often become text nodes.
  • Document children — typically the doctype and the root element (document.documentElement, usually <html>).

📝 Syntax

JavaScript
const kids = node.childNodes;
const first = node.childNodes[0];
const name = node.childNodes[0].nodeName;

Return value

A live NodeList containing the children of the node.

⚖️ childNodes vs children

node.childNodeselement.children
InterfaceNodeElement
ContainsAll child nodes (elements, text, comments)Element children only
Collection typeLive NodeListLive HTMLCollection
Best forFull DOM tree walksWorking with tags / layout

⚡ Quick Reference

GoalCode
Get the listnode.childNodes
Count childrennode.childNodes.length
First child nodenode.childNodes[0] or node.firstChild
Check before loopingif (node.hasChildNodes()) { … }
Elements onlyelement.children
Clear all childrenwhile (node.firstChild) node.removeChild(node.firstChild);
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Node.childNodes.

Returns
NodeList

Live collection

Baseline
widely

Since July 2015

Access
read-only

List is live

Watch for
#text

Whitespace nodes

Examples Gallery

Examples follow MDN Node.childNodes patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Loop children and notice text nodes from whitespace.

Example 1 — Loop with hasChildNodes()

MDN-style: check for children, then walk the live list.

JavaScript
const para = document.querySelector("p");

if (para.hasChildNodes()) {
  const children = para.childNodes;
  for (const node of children) {
    console.log(node.nodeName, node.nodeType);
    // NOTE: List is live! Adding or removing children changes length
  }
}
Try It Yourself

How It Works

hasChildNodes() avoids empty loops. nodeType 1 is an element; 3 is a text node.

Example 2 — Whitespace Text Nodes

Pretty-printed HTML often makes childNodes[0] a #text node.

JavaScript
// HTML source:
// <ul id="list">
//   <li>One</li>
// </ul>

const list = document.getElementById("list");
console.log(list.childNodes[0].nodeName); // often "#text" (newline/indent)
console.log(list.children[0].nodeName);   // "LI"
Try It Yourself

How It Works

Browsers keep whitespace from the source as text nodes. children skips them and returns only elements.

📈 Filter, Clear & Document

Compare collections, empty a node, and inspect document.

Example 3 — Count childNodes vs children

See how many extra text/comment nodes inflate childNodes.length.

JavaScript
const box = document.getElementById("box");
console.log("childNodes:", box.childNodes.length);
console.log("children:", box.children.length);

for (const node of box.childNodes) {
  if (node.nodeType === Node.ELEMENT_NODE) {
    console.log("element:", node.tagName);
  }
}
Try It Yourself

How It Works

Filter with Node.ELEMENT_NODE when you must use childNodes but only want elements.

Example 4 — Remove All Children

MDN pattern: drain the live list with firstChild / removeChild.

JavaScript
const box = document.getElementById("box");

while (box.firstChild) {
  // The list is LIVE so it re-indexes each call
  box.removeChild(box.firstChild);
}

console.log(box.childNodes.length); // 0
Try It Yourself

How It Works

Each removal shrinks the live list. Using a reverse index loop over childNodes while mutating can also work; the while (firstChild) pattern is simple and clear.

Example 5 — document.childNodes

The document usually has a doctype node and the root <html> element.

JavaScript
for (const node of document.childNodes) {
  console.log(node.nodeName, node.nodeType);
}
console.log("documentElement:", document.documentElement.nodeName);
// Often: html document type (#document-type / 10) and HTML (1)
Try It Yourself

How It Works

MDN: the document object itself has two common children—the doctype declaration and the root element (documentElement).

🚀 Common Use Cases

  • Full tree walks — inspect every child including comments and text.
  • Custom serializers — rebuild markup from node types and names.
  • Clearing containers — empty a panel before rendering new content.
  • Teaching the DOM — show why whitespace appears as #text.
  • Prefer children for UI — when you only need element tags for layout logic.

🧠 How childNodes Works

1

Read the property

Engine returns the live NodeList for that parent.

Access
2

Include all kinds

Elements, text (often whitespace), and comments.

Nodes
3

Stay live

DOM mutations update the same list object.

Live
4

Use node properties

Read nodeName, nodeType, textContent, and so on.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Live list: mutating children while iterating needs care.
  • Whitespace text nodes are normal in indented HTML.
  • Prefer Element.children when you only want element tags.
  • Related: baseURI, EventTarget, JavaScript hub.

Universal Browser Support

Node.childNodes is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Node.childNodes

Safe for production. Remember the NodeList is live and may include whitespace text nodes from your HTML source.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Long-standing support in legacy IE
Full support
childNodes Excellent

Bottom line: Use childNodes for every child node type; use Element.children when you only need elements.

Conclusion

Node.childNodes gives you a live list of every direct child—including text and comments. Watch for whitespace nodes, prefer children for element-only work, and clear containers with a live-safe loop when needed.

Continue with firstChild, baseURI, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Call hasChildNodes() before assuming children exist
  • Use children / firstElementChild for element-only work
  • Check nodeType when filtering childNodes
  • Clear with while (node.firstChild) removeChild(…)
  • Remember the list is live when adding/removing inside loops

❌ Don’t

  • Assume childNodes[0] is always an element
  • Treat the NodeList like a frozen array snapshot
  • Confuse DOM Node with the Node.js runtime
  • Ignore comments/text when serializing the full tree
  • Assign to childNodes (read-only property)

Key Takeaways

Knowledge Unlocked

Five things to remember about childNodes

Live children of every node type.

5
Core concepts
📄 02

All node kinds

el + text + !--

Content
03

Whitespace

#text traps

Gotcha
⚖️ 04

vs children

elements only

Compare
🗑️ 05

Clear safely

while firstChild

Pattern

❓ Frequently Asked Questions

A live NodeList of the node’s direct children. Index 0 is the first child. The list includes element nodes, text nodes, and comment nodes — not only HTML elements.
No. MDN marks Node.childNodes as Baseline Widely available (since July 2015). It is a standard DOM property — not Deprecated, Experimental, or Non-standard.
The list updates automatically when children are added or removed. Changing the DOM changes length and indexes. Several reads of childNodes return the same NodeList object.
Browsers insert text nodes for whitespace in the HTML source (newlines and indentation). So childNodes[0] may be whitespace, not the first element you wrote in the markup.
Use children when you want only element children (HTMLCollection of elements). Use childNodes when you need every kind of child, including text and comments.
A common pattern is while (box.firstChild) { box.removeChild(box.firstChild); }. Because the list is live, it re-indexes as you remove nodes.
Did you know?

Calling node.childNodes multiple times returns the same NodeList object. That is why a stored reference stays up to date after you append or remove children—you are not holding a copied array.

Next: firstChild

Read the first child node—or null when empty.

firstChild →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful