JavaScript Node childNodes Property

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is childNodes?

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>
GoalPrefer
All child node typeschildNodes
Element children onlychildren
First / last childfirstChild / lastChild

How to Use childNodes

Read the live list, then loop or inspect nodeName / nodeType.

  1. Get a parent node (often an element).
  2. Optionally check hasChildNodes().
  3. Read parent.childNodes (same NodeList on repeat reads).
  4. Loop with for…of — remember the list is live.
PieceRequired?Role
parentYesNode whose children you read
Return—Live NodeList
Writable?NoRead-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)

Cheat Sheet

Keep these facts handy when walking child lists.

TopicDetail
TypeLive NodeList
Index0 = first child
IncludesElements, text, comments, …
vs childrenElements only
WhitespaceOften creates leading text nodes
Same listRepeat reads return the same object
StandardBaseline Widely available (since July 2015)

Live Preview

Inspect #box. Compare childNodes.length with children.length (whitespace counts).

Box
A B

Browser demo of Node.childNodes vs children.

Result
Choose an action, then Run…

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);
  }
}
Try It Yourself

How It Works

  1. Guard with hasChildNodes. Skip empty parents.
  2. Read childNodes. Live list of direct children.
  3. for…of each node. Works on NodeList.
  4. Log nodeName. See elements vs #text.

Example 2 — childNodes vs children

Same parent, different counts when whitespace exists.

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

How It Works

  1. childNodes counts all. Text/comments included.
  2. children counts elements. HTMLCollection of tags only.
  3. Whitespace raises childNodes. Pretty HTML adds text nodes.
  4. Pick the API that matches what you mean by “child.”

Example 3 — First Item May Be Text

MDN note: childNodes[0] can be whitespace.

JavaScript
const box = document.getElementById("box");
console.log(box.childNodes[0].nodeName);
console.log(box.children[0].nodeName);
Try It Yourself

How It Works

  1. Read childNodes[0]. Often a text node from newlines.
  2. Read children[0]. First element child.
  3. Compare names. #text vs SPAN.
  4. Use firstElementChild when you want the first tag.

Example 4 — The List Is Live

Appending a child increases length on the same NodeList.

JavaScript
const box = document.getElementById("box");
const list = box.childNodes;
console.log(list.length);

box.appendChild(document.createTextNode("!"));
console.log(list.length); // grew — same list object
Try It Yourself

How It Works

  1. Store childNodes once. Keep a reference to the NodeList.
  2. Log length. Snapshot of current children.
  3. Append a node. DOM updates.
  4. Length changes. Live list — no re-query needed.

Example 5 — Remove All Children

MDN pattern: loop while firstChild exists.

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

while (box.firstChild) {
  box.removeChild(box.firstChild);
}
console.log(box.childNodes.length); // 0
Try It Yourself

How It Works

  1. While firstChild exists. Still has children.
  2. removeChild the first. Live list re-indexes.
  3. Repeat until empty. No leftover text nodes.
  4. Length is 0. Parent is cleared.

Edge Cases & Pitfalls

Check these before treating childNodes like an element-only list.

whitespace

Index 0 may be a text node

MDN: browsers insert text nodes for whitespace in source markup.

live

Live list while looping

Adding/removing during iteration changes length and indexes — plan for it.

elements

Need tags only → children

Use Element.children for an element-only collection.

objects

Items are nodes, not strings

Read nodeName, textContent, etc. — the list holds node objects (MDN).

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
childNodes Excellent

Bottom line: Safe everyday DOM API. Expect text nodes from pretty HTML, and remember the list is live. See the MDN childNodes reference.

Performance Notes

ApproachCostBest for
Read childNodesO(1) list handleAccess live children
Loop all childNodesO(n) incl. textFull node walk
Loop childrenO(elements)Element-only UI work

Prefer children / querySelector when you do not need text nodes — fewer items to visit in pretty-printed markup.

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.

Next: firstChild

Learn the first node in the childNodes list.

firstChild tutorial →

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.

8 people found this page helpful