Document.childElementCount is a read-only instance property (from the ParentNode mixin) that returns how many element children sit directly under the document node. Learn MDN’s typical value of 1 (<html>), how it differs from childNodes, and when to use Element.childElementCount instead—with five examples and try-it labs.
01
Kind
Read-only
02
Returns
number
03
Counts
Elements only
04
Typical
1 (html)
05
Same as
children.length
06
Status
Baseline widely
Fundamentals
Introduction
The DOM tree starts at the Document node. Its direct children include the doctype, comments, and usually one element: <html>. childElementCount ignores non-element nodes and counts only those element children.
MDN’s example: on a normal page, document.children is an HTMLCollection with a single <html> entry, and document.childElementCount is 1.
💡
Document vs Element
To count items inside a <ul>, <div>, or <body>, use Element.childElementCount. This page covers the same property on Document itself.
document.documentElement is shorthand for the document’s root element when count is 1.
Example 5 — Document vs body.childElementCount
Document count is almost always 1; body count reflects visible page structure.
JavaScript
console.log("document:", document.childElementCount);
console.log("body:", document.body.childElementCount);
// body counts direct element children (header, main, script, etc.)
Document.childElementCount is marked Baseline Widely available on MDN (since April 2018). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Document.childElementCount
Read-only count of direct element children on Document — typically 1 (the html element).
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in legacy IE
Full support
Document.childElementCountExcellent
Bottom line: Use document.childElementCount for document-root element counts. For lists and containers inside the page, use Element.childElementCount on the specific parent node.
Wrap Up
Conclusion
Document.childElementCount tells you how many element nodes sit directly under the document—almost always one <html> on standard pages. Use it to understand document-root structure; use Element.childElementCount for everyday container counting.
Use document.documentElement to access the html root directly
Use element.childElementCount for container/list counts
Compare with children.length when you need the collection too
Prefer childElementCount over filtering childNodes manually
Teach beginners the document vs element distinction clearly
❌ Don’t
Expect a large count from document.childElementCount on normal pages
Confuse document count with total elements in the page tree
Count list items via document.childElementCount
Assume doctype is included in the count
Try to assign a number to childElementCount
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.childElementCount
Direct element-child count — usually 1 on HTML pages.
5
Core concepts
🔢01
Returns
number
API
✓02
Status
baseline
Standard
📄03
Typical
1 (html)
MDN
🔍04
vs
childNodes
Compare
📦05
Containers
Element API
Use case
❓ Frequently Asked Questions
The number of direct child elements of the document — element nodes only, not text, comments, or the doctype. On a typical HTML page the value is 1 (the html element).
No. MDN marks Document.childElementCount as Baseline Widely available (since April 2018). It comes from the ParentNode mixin on Document.
Same property name and behavior, different node: document.childElementCount counts the document's direct element children; element.childElementCount counts an element's direct element children. See the Element tutorial for container use cases.
Yes. childElementCount equals the length of the document.children HTMLCollection — both count direct element children only.
MDN: document.children is usually an HTMLCollection containing a single html element — the document's only direct element child.
No. It is read-only. Change the count by adding or removing direct element children of the document (rare in normal apps).
Did you know?
The same childElementCount property exists on DocumentFragment and Element through the shared ParentNode mixin—only the node you call it on changes what gets counted.