JavaScript Document childElementCount Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

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

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.

Related tutorials: body, Element.children, appendChild().

Understanding Document.childElementCount

A read-only instance property from the ParentNode interface, exposed on Document. Its value is the number of direct child Element nodes.

  • Value — non-negative integer (typically 1 for HTML documents).
  • Read-only — add/remove element children to change the count.
  • Elements only — excludes doctype, comments, and text nodes.
  • Direct children — does not count descendants inside <html>.
  • Equivalentdocument.children.length on supporting browsers.

📝 Syntax

JavaScript
document.childElementCount

Value

A number — count of direct element children of the document.

MDN example

JavaScript
document.children;
// HTMLCollection, usually containing an <html> element

document.childElementCount;
// 1

⚡ Quick Reference

GoalCode / note
Count document element childrendocument.childElementCount
Same via collectiondocument.children.length
First element childdocument.children[0] or document.documentElement
Count inside bodydocument.body.childElementCount
Includes doctype?No (doctype is not an element child)
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.childElementCount.

Type
number

Read-only

Scope
direct only

Not descendants

Typical
1

html child

Status
baseline

ParentNode

📋 childElementCount vs childNodes.length

childElementCountchildNodes.length
Node typesElements onlyElements, text, comments, doctype, etc.
On DocumentUsually 1Usually 2+ (doctype + html)
Use whenCount element slots at document rootNeed every child node type
MDN on DocumentThis propertyNode.childNodes (broader)

Examples Gallery

Examples follow MDN Document: childElementCount. Use View Output or Try It Yourself for each case.

📚 Getting Started

Read the document-level element child count.

Example 1 — MDN: document.childElementCount

Log the count and inspect document.children.

JavaScript
console.log(document.childElementCount);
console.log(document.children[0].tagName); // "HTML"
Try It Yourself

How It Works

The document’s only direct element child is usually the root <html> element.

Example 2 — Match document.children.length

Verify both APIs report the same element-child count.

JavaScript
console.log(document.childElementCount);
console.log(document.children.length);
console.log(document.childElementCount === document.children.length);
Try It Yourself

How It Works

childElementCount is a convenience number; children gives you the live collection.

📈 Compare & Practical Counts

Contrast with childNodes and count inside body.

Example 3 — Compare with childNodes.length

Show that doctype and other non-element nodes inflate childNodes.

JavaScript
console.log("childElementCount:", document.childElementCount);
console.log("childNodes.length:", document.childNodes.length);
// childNodes often larger (includes doctype node)
Try It Yourself

How It Works

Use childElementCount when you care about element children only—not doctype or comment nodes.

Example 4 — Relate to document.documentElement

When count is 1, the sole element child is the html root.

JavaScript
console.log(document.childElementCount);
console.log(document.children[0] === document.documentElement);
console.log(document.documentElement.tagName);
Try It Yourself

How It Works

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.)
Try It Yourself

How It Works

For list-like UI counts, use Element.childElementCount on the container element—not on Document.

🚀 Common Use Cases

  • DOM introspection — confirm document root structure in devtools scripts.
  • Teaching the DOM — contrast document vs element child counts.
  • Sanity checks — verify document.children[0] exists before access.
  • Not for list UI — use Element.childElementCount on containers instead.
  • Parser edge cases — unusual documents may have more than one root element child.
  • Pair with documentElement — when count is 1, grab the html root directly.

🧠 How childElementCount Works on Document

1

Document node created

Parser attaches doctype and builds the tree.

Parse
2

<html> added as child

Root element becomes a direct child of Document.

Element
3

Property counts elements only

Doctype/comments ignored — count is typically 1.

Filter
4

Read anytime

document.childElementCount updates if direct element children change.

📝 Notes

  • MDN: Baseline Widely available (since April 2018) — from ParentNode mixin.
  • Read-only — mutate the DOM to change the value.
  • Counts direct element children only—not nested descendants.
  • MDN: for a specific element’s children, see Element.childElementCount.
  • Usually equals document.children.length.
  • Related: Element childElementCount, body, Element.children.

Universal Browser Support

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

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE
Full support
Document.childElementCount Excellent

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.

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.

Continue with children, ownerDocument, Element childElementCount, or the JavaScript hub.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about document.childElementCount

Direct element-child count — usually 1 on HTML pages.

5
Core concepts
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.

Next: children

Learn the live HTMLCollection of document element children.

children →

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.

6 people found this page helpful