JavaScript Document children Property

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

What You’ll Learn

Document.children is a read-only instance property (from the ParentNode mixin) that returns a live HTMLCollection of the document’s direct element children. Learn MDN’s usual [<html>] result, how to access items, how it differs from childNodes, and when to use Element.children instead—with five examples and try-it labs.

01

Kind

Read-only

02

Returns

HTMLCollection

03

Contains

Elements only

04

Typical

[<html>]

05

Live

Updates with DOM

06

Status

Baseline widely

Introduction

The Document node sits at the top of the DOM. Its direct children can include a doctype, comments, and element nodes. document.children filters that list down to element children only.

MDN notes that for HTML documents this collection usually contains just the root <html> element—the document’s only direct element child. That is why document.children.length and document.childElementCount are both typically 1.

💡
Document vs Element

To list items inside a <ul>, <div>, or <body>, use Element.children. This page covers the same property on Document itself.

Related tutorials: childElementCount, body, appendChild().

Understanding Document.children

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

  • ValueHTMLCollection (live).
  • Accessdocument.children[i] or document.children.item(i).
  • Elements only — excludes doctype, comments, and text nodes.
  • Direct children — does not include descendants inside <html>.
  • Empty case — if there are no element children, length is 0.

📝 Syntax

JavaScript
document.children

Value

An HTMLCollection of the document’s direct element children. On a typical HTML page this is usually just the root <html> element.

MDN example

JavaScript
document.children;
// HTMLCollection [<html>]
// Usually only contains the root <html> element

⚡ Quick Reference

GoalCode / note
Get element childrendocument.children
Count themdocument.children.length or childElementCount
First element childdocument.children[0] or document.documentElement
item() accessdocument.children.item(0)
Children of bodydocument.body.children
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.children.

Type
HTMLCollection

Live list

Scope
direct only

Not descendants

Typical
[<html>]

One root

Status
baseline

ParentNode

📋 children vs childNodes

document.childrendocument.childNodes
Node typesElements onlyElements, text, comments, doctype, etc.
Collection typeHTMLCollectionNodeList
On DocumentUsually one htmlOften doctype + html
Use whenYou want element children onlyYou need every child node type

Examples Gallery

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

📚 Getting Started

Inspect the document-level element children collection.

Example 1 — MDN: Inspect document.children

Log the collection and the first child’s tag name.

JavaScript
console.log(document.children);
// HTMLCollection [<html>]
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 — Index and item() Access

Read the same child two ways and compare with documentElement.

JavaScript
const first = document.children[0];
const viaItem = document.children.item(0);

console.log(first === viaItem); // true
console.log(first === document.documentElement); // true
Try It Yourself

How It Works

Array-style indexing and item(i) both return the element at that position in the live collection.

📈 Count, Compare & Body Children

Relate length, childNodes, and everyday Element.children usage.

Example 3 — Match childElementCount

Collection length equals the dedicated count property.

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

How It Works

Use children when you need the elements; use childElementCount when you only need the number.

Example 4 — Compare with childNodes

Show that non-element nodes inflate childNodes but not children.

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

How It Works

Prefer children when you only care about element children at the document root.

Example 5 — Document vs body.children

Document children are almost always just html; body children reflect page structure.

JavaScript
console.log("document:", document.children.length);
console.log("body:", document.body.children.length);
for (const el of document.body.children) {
  console.log(el.tagName.toLowerCase());
}
Try It Yourself

How It Works

For lists and layout containers, use Element.children on the parent element—not on Document.

🚀 Common Use Cases

  • DOM introspection — confirm the document root element exists and is html.
  • Teaching the DOM — contrast document-level and element-level children.
  • Safe root access — check children.length before using children[0].
  • Not for list UI — use Element.children on containers instead.
  • Parser edge cases — unusual documents may expose more than one root element child.
  • Pair with documentElement — prefer document.documentElement when you only need the html root.

🧠 How document.children Fits the DOM

1

Document node created

Parser attaches doctype and builds the tree.

Parse
2

<html> becomes a child

Root element is a direct child of Document.

Element
3

children filters elements

Doctype/comments ignored — collection is typically [<html>].

Filter
4

Live collection stays current

document.children updates if direct element children change.

📝 Notes

  • MDN: Baseline Widely available (since October 2017) — from ParentNode mixin.
  • Read-only live HTMLCollection — mutate the DOM to change its contents.
  • Lists direct element children only—not nested descendants.
  • MDN: for children of a specific HTML element, see Element.children.
  • If there are no element children, the collection is empty (length === 0).
  • Related: childElementCount, Element.children, body.

Universal Browser Support

Document.children is marked Baseline Widely available on MDN (since October 2017). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.children

Live HTMLCollection of direct element children on Document — typically just the html root.

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.children Excellent

Bottom line: Use document.children for document-root element children. For lists and containers inside the page, use Element.children on the specific parent node.

Conclusion

Document.children gives you a live list of element nodes directly under the document—almost always just <html> on standard pages. Use it to understand document-root structure; use Element.children for everyday container listing.

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

💡 Best Practices

✅ Do

  • Use document.documentElement when you only need the html root
  • Use element.children for container/list children
  • Prefer children over filtering childNodes manually
  • Check length before indexing if the document may be unusual
  • Teach beginners the document vs element distinction clearly

❌ Don’t

  • Expect a large collection from document.children on normal pages
  • Confuse document children with every element in the page tree
  • List menu items via document.children
  • Assume doctype is included in the collection
  • Try to assign a new array to document.children

Key Takeaways

Knowledge Unlocked

Five things to remember about document.children

Live element-child collection — usually just the html root.

5
Core concepts
02

Status

baseline

Standard
🏠03

Typical

[<html>]

MDN
🔍04

vs

childNodes

Compare
📦05

Containers

Element.children

Use case

❓ Frequently Asked Questions

A live HTMLCollection of the document's direct child elements only. On a typical HTML page it usually contains just the root html element.
No. MDN marks Document.children as Baseline Widely available (since October 2017). It comes from the ParentNode mixin on Document.
Same property name and behavior, different node: document.children lists the document's direct element children; element.children lists an element's direct element children. Use Element.children for lists, cards, and containers inside the page.
children includes only Element nodes. childNodes includes every node type — elements, text, comments, and often the doctype on Document.
On a normal HTML page, children[0] is usually the same node as document.documentElement (the html element). documentElement is the convenient shortcut to that root.
No. It is read-only. You can read length and items, but you change the collection by adding or removing direct element children of the document (rare in normal apps).
Did you know?

children is a live collection: if the document’s direct element children change, the same HTMLCollection object updates automatically. You do not need to re-query document.children to see the new length.

Next: compatMode

Learn how to detect quirks mode vs standards mode.

compatMode →

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