The Node.childNodes property returns a liveNodeList 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
Fundamentals
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.
Concept
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>).
Foundation
📝 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.
Compare
⚖️ childNodes vs children
node.childNodes
element.children
Interface
Node
Element
Contains
All child nodes (elements, text, comments)
Element children only
Collection type
Live NodeList
Live HTMLCollection
Best for
Full DOM tree walks
Working with tags / layout
Cheat Sheet
⚡ Quick Reference
Goal
Code
Get the list
node.childNodes
Count children
node.childNodes.length
First child node
node.childNodes[0] or node.firstChild
Check before looping
if (node.hasChildNodes()) { … }
Elements only
element.children
Clear all children
while (node.firstChild) node.removeChild(node.firstChild);
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 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
Hands-On
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
}
}
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
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)
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
childNodesExcellent
Bottom line: Use childNodes for every child node type; use Element.children when you only need elements.
Wrap Up
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.
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)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about childNodes
Live children of every node type.
5
Core concepts
🗃️01
Live NodeList
auto-updates
API
📄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 sameNodeList object. That is why a stored reference stays up to date after you append or remove children—you are not holding a copied array.