JavaScript Node hasChildNodes() Method

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

What Is hasChildNodes()?

Node.hasChildNodes() returns a Boolean: true if the node has one or more child nodes, false otherwise (MDN). Children include elements, text, and comments — not only tags.

Baseline · Widely available: not Deprecated, Experimental, or Non-standard (since about July 2015).

Whitespace counts: newlines/spaces between tags become text nodes, so a “visually empty” element can still return true.

Remember
node.hasChildNodes()

→ true  if childNodes.length > 0
→ false if no children

includes: Element · Text · Comment · …

MDN pattern:
if (foo.hasChildNodes()) {
  // use foo.childNodes
}
UsePrefer
Any child node?node.hasChildNodes()
Only element children?element.children.length
List every childnode.childNodes

How to Use hasChildNodes()

Call it on a node, then branch before reading or looping childNodes.

  1. Get the node (for example with getElementById).
  2. Call node.hasChildNodes().
  3. If true, use childNodes (or clear/walk children).
  4. If you only care about elements, compare with children.length instead.
PieceRequired?Role
Receiver nodeYesNode to inspect
ParametersNone—
Return value—true / false

Pseudocode

Pseudocode
if node.hasChildNodes():
    // at least one child (any type)
    use node.childNodes
else:
    // empty of all node types

# element-only emptiness:
if element.children.length === 0:
    // no element children (text may still exist)

Cheat Sheet

Keep these facts handy when checking whether a node is empty.

TopicDetail
Syntaxnode.hasChildNodes()
Returnstrue · false
Equivalentnode.childNodes.length > 0
CountsElements, text, comments, …
WhitespaceCan make “empty” HTML return true
Element-onlyelement.children.length
Status (MDN)Baseline Widely available

Live Preview

Compare filled, empty, whitespace-only, and element-only checks on small demo nodes.

Browser demo of Node.hasChildNodes() — remember whitespace text nodes.

Result
Loading…

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — MDN Guard

Only use childNodes when the node has children.

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

if (foo.hasChildNodes()) {
  console.log(foo.childNodes.length);
} else {
  console.log("no children");
}
Try It Yourself

How It Works

  1. Call the Boolean API. MDN’s recommended gate.
  2. Branch on true. Safe to read childNodes.
  3. Else means empty. No element, text, or comment children.
  4. Length confirms. childNodes.length is at least 1.

Example 2 — Truly Empty

A self-closing / empty element with no text returns false.

JavaScript
const empty = document.getElementById("empty");
console.log(empty.hasChildNodes()); // false
console.log(empty.childNodes.length); // 0
Try It Yourself

How It Works

  1. No child nodes. Not even a text node.
  2. Boolean is false. Matches length 0.
  3. Write empty carefully. <div></div> with a newline inside is not empty.
  4. Good clear target. Nothing to remove.

Example 3 — Whitespace Text Node

Pretty-printed HTML often inserts text children you do not see.

JavaScript
const box = document.getElementById("ws");
console.log(box.hasChildNodes()); // true
console.log(box.childNodes[0].nodeName); // #text
console.log(box.children.length); // 0
Try It Yourself

How It Works

  1. Newline/space → text node. Counts for hasChildNodes.
  2. nodeName is #text. Not an element.
  3. children.length stays 0. Element-only collection.
  4. Pick the right check. “Any nodes?” vs “any elements?”

Example 4 — vs children.length

Same node, two emptiness questions.

JavaScript
const el = document.getElementById("ws");
console.log("hasChildNodes: " + el.hasChildNodes());
console.log("childNodes.length: " + el.childNodes.length);
console.log("children.length: " + el.children.length);
Try It Yourself

How It Works

  1. hasChildNodes. Any child type.
  2. childNodes.length. Same idea as a count.
  3. children.length. Elements only.
  4. UI “empty”. Often means children.length === 0.

Example 5 — Clear If Any

Remove all children only when the container is not empty.

JavaScript
function clearAll(node) {
  if (!node.hasChildNodes()) {
    return "already empty";
  }
  while (node.firstChild) {
    node.removeChild(node.firstChild);
  }
  return "cleared; hasChildNodes=" + node.hasChildNodes();
}

console.log(clearAll(document.getElementById("foo")));
Try It Yourself

How It Works

  1. Early return. Skip work when already empty.
  2. Remove via firstChild. Classic clear loop.
  3. Re-check. Should be false after clearing.
  4. Removes text too. Whitespace children are cleared as well.

Edge Cases & Pitfalls

Check these before treating hasChildNodes() as “has visible content.”

ws

Whitespace is a child

Formatted HTML often inserts #text nodes. Use children.length when you mean elements only.

vs

Not the same as “has HTML tags”

true can mean only a comment or text node — not necessarily an element child.

empty

Write empty markup carefully

<div></div> on one line is empty; a newline between the tags usually is not.

api

Prefer the Boolean over reinventing it

hasChildNodes() is clearer than childNodes.length > 0 for the same question.

Browser Support

Node.hasChildNodes() is Baseline Widely available (MDN, since about July 2015). Safe everywhere you use the DOM.

✓ Baseline · Widely available

Node.hasChildNodes()

Supported across Chrome, Firefox, Safari, Edge, Opera, and legacy IE. No options — just a Boolean.

99% Browser support
Google Chrome hasChildNodes() · full support
Full support
Microsoft Edge hasChildNodes() · Chromium
Full support
Apple Safari hasChildNodes() · full support
Full support
Mozilla Firefox hasChildNodes() · full support
Full support
Opera hasChildNodes() · full support
Full support
Internet Explorer hasChildNodes() · legacy
Legacy support
Node.hasChildNodes() Excellent

Bottom line: Use freely. Remember whitespace when interpreting “empty.”

Performance Notes

ApproachCostBest for
hasChildNodes()NegligibleGuards before loops / clears
childNodes.length > 0SimilarSame question, less clear
Walk all children to “see if any”WastefulAvoid — use the Boolean

Call it when you need a yes/no. Do not poll it in animation frames.

Key Takeaways

  • Role: Boolean — does this node have any children?
  • Counts all types: elements, text, comments, …
  • Whitespace: can make “empty-looking” HTML return true.
  • Elements only: use children.length instead.

One line: hasChildNodes() answers “any child nodes at all?” — including invisible whitespace.

Frequently Asked Questions

A Boolean: true if the node has one or more child nodes, false if it has none. Children include elements, text nodes, and comments (MDN).
No. MDN marks it Baseline Widely available (since about July 2015). It is not Deprecated, Experimental, or Non-standard.
Yes. Spaces and newlines between tags become text nodes, so an element that looks empty in HTML can still return true.
hasChildNodes and childNodes include all node types. Element.children only counts element children and ignores text/comments.
Yes for practical purposes. hasChildNodes() is the dedicated Boolean API for that check.
Before looping childNodes, clearing children, or deciding whether a container has any nodes at all (remember whitespace).

Next: insertBefore()

Learn how node.insertBefore() inserts a child before a reference node.

insertBefore 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.

10 people found this page helpful