Document.childElementCount is a read-only number: how many direct child elements the document has (MDN). On a typical HTML page it is 1—the <html> root.
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax
Use document.childElementCount. Start with this line.
JavaScript
document.childElementCount
Call
What it does
document.childElementCount
Primary signature.
Built into the browser. Read or assign document.childElementCount — no import required.
Library declarations
JavaScript
document.childElementCount
Parameters
Parameter
Description
childElementCount
Required. Read-only number of direct element children
children
Optional. Live HTMLCollection of those elements
Return Value
Return Value
Description
*
The current value of document.childElementCount.
JavaScript Examples
Example 1 — Read document.childElementCount
Log the count and the first element child’s tag name (MDN pattern).
Element filter.childElementCount skips doctype, text, and comments.
Full child list.childNodes includes every node type.
Typical gap. Doctype + html → length 2, element count 1.
Pick the right tool. Want elements only? Use childElementCount / children.
Compatibility
Browser Support
Document.childElementCount is Baseline Widely available on MDN (since April 2018). It is part of the ParentNode mixin shared with Element and DocumentFragment.
✓ Baseline · Widely available
Document.childElementCount
Read-only count of direct element children on a Document — usually 1 for HTML.
99%Browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
childElementCountExcellent
Bottom line: Safe for all modern browsers. Remember: document-level count is rarely useful for UI — prefer Element.childElementCount on the container you care about.
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.
Yes. childElementCount equals the length of the document.children HTMLCollection — both count direct element children only.
MDN: document.children usually contains a single html element — the document's only direct element child.
No. It is read-only. The count changes only when direct element children of the document are added or removed (rare in normal apps).