Element.childElementCount is a read-only instance property that returns how many element children a node has. Learn how it ignores text nodes, how it compares to childNodes.length, and how to use it for empty checks—with five examples and try-it labs.
01
Kind
Instance property
02
Access
Read-only
03
Type
Number
04
Counts
Element children
05
Status
Baseline widely
06
Same as
children.length
Fundamentals
Introduction
A container like a sidebar or list often needs a quick answer: “Does this have any child elements?” You do not always need the full list—just the count.
childElementCount returns that number. It counts only elements (tags), not text or comments sitting between them.
JavaScript
const sidebar = document.getElementById("sidebar");
if (sidebar.childElementCount > 0) {
// Do something
}
💡
Beginner tip
Pretty-printed HTML often inserts whitespace text nodes. Those inflate childNodes.length but leave childElementCount unchanged.
Concept
Understanding the Property
MDN: the Element.childElementCount read-only property returns the number of child elements of this element.
Read-only — change the DOM to change the count.
Elements only — ignores text and comment nodes.
Matches children.length — same element-only view.
Baseline — widely available since July 2015 (MDN).
Foundation
📝 Syntax
JavaScript
childElementCount
Value
A number: how many child elements this element currently has.
Item
Detail
Type
number (unsigned long)
Access
Read-only
Counts
Element children only
Related
children, childNodes, firstElementChild
⚠️
Elements vs nodes
childElementCount === children.length, but it is often smaller than childNodes.length when whitespace text nodes exist.
Pattern
📋 MDN Sidebar Example Shape
MDN checks whether a sidebar has any element children before doing work:
The property always reflects the current DOM. Removing children lowers the count the same way.
Example 5 — Support Snapshot
Feature-detect and remember the element-only rule.
JavaScript
console.log({
supported: "childElementCount" in Element.prototype,
counts: "element children only",
tip: "Same idea as children.length — ignores text nodes",
status: "Baseline Widely available (MDN)"
});
{
"supported": true,
"counts": "element children only",
"tip": "Same idea as children.length — ignores text nodes",
"status": "Baseline Widely available (MDN)"
}
How It Works
Use this property whenever you need a fast, reliable element tally.
Applications
🚀 Common Use Cases
Empty-state checks before rendering lists or sidebars.
Badge or counter UI (“3 items”) without building an array.
Comparing container sizes after DOM updates.
Avoiding false “has children” results from whitespace text nodes.
Teaching the difference between nodes and elements.
Under the Hood
🔧 How It Works
1
Parent has mixed children
Elements, text nodes, and maybe comments.
DOM
2
Property filters to elements
Only tags contribute to the count.
Filter
3
You read a number
Fast check without looping the collection.
Read
4
✓
DOM changes update it
Append or remove children and the count stays accurate.
Important
📝 Notes
Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
Read-only — mutate the DOM to change the value.
Equals children.length; usually differs from childNodes.length.
Element.childElementCount is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.childElementCount
Read-only number — count of child elements (ignores text/comment nodes).
BaselineWidely available
Google ChromeSupported
Yes
Microsoft EdgeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (IE9+)
Yes
childElementCountBaseline
Bottom line: Use childElementCount for fast element-only tallies. Prefer it over childNodes.length when whitespace text nodes would confuse empty checks.
Wrap Up
Conclusion
childElementCount is the simplest way to ask how many element children a node has. Use it for empty checks, counters, and anywhere whitespace text nodes would mislead childNodes.length.