Home JavaScript Node hasChildNodes() JavaScript Node hasChildNodes() Method Definition
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.
node.hasChildNodes()
→ true if childNodes.length > 0
→ false if no children
includes: Element · Text · Comment · …
MDN pattern:
if (foo.hasChildNodes()) {
// use foo.childNodes
} Approach
How to Use hasChildNodes() Call it on a node, then branch before reading or looping childNodes.
Get the node (for example with getElementById). Call node.hasChildNodes(). If true, use childNodes (or clear/walk children). If you only care about elements, compare with children.length instead. Piece Required? Role Receiver node Yes Node to inspect Parameters None — Return value — true / false
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) Reference
Cheat Sheet Keep these facts handy when checking whether a node is empty.
Topic Detail Syntax node.hasChildNodes()Returns true · falseEquivalent node.childNodes.length > 0Counts Elements, text, comments, … Whitespace Can make “empty” HTML return true Element-only element.children.lengthStatus (MDN) Baseline Widely available
Try it
Live Preview Compare filled, empty, whitespace-only, and element-only checks on small demo nodes.
Code
JavaScript Examples Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
1. MDN guard 2. Empty node 3. Whitespace 4. vs children 5. Clear if any Example 1 — MDN Guard Only use childNodes when the node has children.
const foo = document.getElementById("foo");
if (foo.hasChildNodes()) {
console.log(foo.childNodes.length);
} else {
console.log("no children");
} 1
(or another length ≥ 1 when children exist) How It Works Call the Boolean API. MDN’s recommended gate.Branch on true. Safe to read childNodes.Else means empty. No element, text, or comment children.Length confirms. childNodes.length is at least 1.Example 2 — Truly Empty A self-closing / empty element with no text returns false.
const empty = document.getElementById("empty");
console.log(empty.hasChildNodes()); // false
console.log(empty.childNodes.length); // 0 How It Works No child nodes. Not even a text node.Boolean is false. Matches length 0.Write empty carefully. <div></div> with a newline inside is not empty.Good clear target. Nothing to remove.Example 3 — Whitespace Text Node Pretty-printed HTML often inserts text children you do not see.
const box = document.getElementById("ws");
console.log(box.hasChildNodes()); // true
console.log(box.childNodes[0].nodeName); // #text
console.log(box.children.length); // 0 How It Works Newline/space → text node. Counts for hasChildNodes.nodeName is #text. Not an element.children.length stays 0. Element-only collection.Pick the right check. “Any nodes?” vs “any elements?”Example 4 — vs children.length Same node, two emptiness questions.
const el = document.getElementById("ws");
console.log("hasChildNodes: " + el.hasChildNodes());
console.log("childNodes.length: " + el.childNodes.length);
console.log("children.length: " + el.children.length); hasChildNodes: true
childNodes.length: 1
children.length: 0 How It Works hasChildNodes. Any child type.childNodes.length. Same idea as a count.children.length. Elements only.UI “empty”. Often means children.length === 0.Example 5 — Clear If Any Remove all children only when the container is not empty.
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"))); cleared; hasChildNodes=false How It Works Early return. Skip work when already empty.Remove via firstChild. Classic clear loop.Re-check. Should be false after clearing.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.
Compatibility
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.
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.”
Analysis
Performance Notes Approach Cost Best for hasChildNodes()Negligible Guards before loops / clears childNodes.length > 0Similar Same question, less clear Walk all children to “see if any” Wasteful Avoid — use the Boolean
Call it when you need a yes/no. Do not poll it in animation frames.
Remember
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 What does Node.hasChildNodes() return? 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).
Is hasChildNodes deprecated or experimental? No. MDN marks it Baseline Widely available (since about July 2015). It is not Deprecated, Experimental, or Non-standard.
Does whitespace count as a child? Yes. Spaces and newlines between tags become text nodes, so an element that looks empty in HTML can still return true.
How is it different from children.length? hasChildNodes and childNodes include all node types. Element.children only counts element children and ignores text/comments.
Is it the same as childNodes.length > 0? Yes for practical purposes. hasChildNodes() is the dedicated Boolean API for that check.
When should I use hasChildNodes? Before looping childNodes, clearing children, or deciding whether a container has any nodes at all (remember whitespace).
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion