The read-only childNodes property returns a live NodeList of direct child nodes(MDN) — elements, text, and comments. Index 0 is the first child.
Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. Whitespace in HTML often becomes text nodes — prefer Element.children for elements only.
Remember
parent.childNodes
→ live NodeList [child0, child1, …]
Includes: Element, Text, Comment, …
Excludes: only looking at HTML tags → use .children
Live = add/remove children → length & indexes update
childNodes[0] may be whitespace text, not your first <div>
Read parent.childNodes (same NodeList on repeat reads).
Loop with for…of — remember the list is live.
Piece
Required?
Role
parent
Yes
Node whose children you read
Return
—
Live NodeList
Writable?
No
Read-only property
Pseudocode
Pseudocode
if parent.hasChildNodes():
for node in parent.childNodes:
// node may be Element, Text, Comment, …
use node.nodeName / node.nodeType
elements only?
for el in parent.children: …
clear all children:
while parent.firstChild:
parent.removeChild(parent.firstChild)
Reference
Cheat Sheet
Keep these facts handy when walking child lists.
Topic
Detail
Type
Live NodeList
Index
0 = first child
Includes
Elements, text, comments, …
vs children
Elements only
Whitespace
Often creates leading text nodes
Same list
Repeat reads return the same object
Standard
Baseline Widely available (since July 2015)
Try it
Live Preview
Inspect #box. Compare childNodes.length with children.length (whitespace counts).
Box
AB
Browser demo of Node.childNodes vs children.
Result
Choose an action, then Run…
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Loop childNodes
MDN-style: check for children, then iterate the live list.
JavaScript
const para = document.getElementById("para");
if (para.hasChildNodes()) {
for (const node of para.childNodes) {
console.log(node.nodeName);
}
}
Read nodeName, textContent, etc. — the list holds node objects (MDN).
Compatibility
Browser Support
Node.childNodes is marked Baseline Widely available on MDN (since July 2015).
✓ Baseline · Widely available
Node.childNodes
Live NodeList of direct children (elements, text, comments). Whitespace often creates text nodes. Prefer Element.children for elements only.
100%Widely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
childNodesExcellent
Bottom line: Safe everyday DOM API. Expect text nodes from pretty HTML, and remember the list is live. See the MDN childNodes reference.
Analysis
Performance Notes
Approach
Cost
Best for
Read childNodes
O(1) list handle
Access live children
Loop all childNodes
O(n) incl. text
Full node walk
Loop children
O(elements)
Element-only UI work
Prefer children / querySelector when you do not need text nodes — fewer items to visit in pretty-printed markup.
Remember
Key Takeaways
Live NodeList: all direct children, every node type.
Whitespace:[0] may be #text.
Elements only: use children.
Clear with while firstChild — live-list friendly.
One line:childNodes is the live list of every direct child — including the whitespace you forgot about.
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 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. 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.