Document.firstElementChild is a read-only instance property that returns the document’s first child Element, or null. For HTML this is usually the root <html> element. Learn how it differs from firstChild and documentElement, plus five examples with try-it labs.
01
Kind
Read-only
02
Returns
Element | null
03
HTML
Usually <html>
04
vs
firstChild
05
Related
documentElement
06
Status
Baseline widely
Fundamentals
Introduction
A Document node can have several kinds of children: a doctype, comments, and the root element. When you only care about the first element child, use document.firstElementChild.
MDN: for HTML documents this is usually the only child element—the root <html>. For walking children of a specific tag (like the first <li> in a list), use Element.firstElementChild instead.
💡
Why not always use firstChild?
document.firstChild often returns the DocumentType from <!DOCTYPE html>, not <html>. firstElementChild skips non-element nodes so beginners get the element they expect.
Always null-check if you write libraries that might run against empty documents.
Example 5 — Element.firstElementChild Inside the Tree
MDN points to the Element twin for children of specific elements.
JavaScript
const list = document.getElementById("list");
console.log(list.firstElementChild.textContent);
// "First (1)" — skips whitespace text nodes between tags
Document.firstElementChild 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.firstElementChild
Read-only first child Element of the document — usually the root <html> element.
WidelyAvailable
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 modern IE / Edge legacy paths
Full support
Document.firstElementChildBaseline support
Bottom line: Use firstElementChild when you need the first Element under Document. Prefer document.documentElement for root-html intent, and Element.firstElementChild for nested trees.
Wrap Up
Conclusion
Document.firstElementChild is the standard way to read the document’s first element child—almost always <html> on HTML pages. Use it to avoid doctype/text-node surprises from firstChild, and reach for documentElement when you mean the root by name.
Prefer document.documentElement for root-html intent
Use el.firstElementChild inside components / lists
Null-check in generic DOM utilities
Compare with firstChild when teaching node types
❌ Don’t
Assume firstChild is always <html>
Call element methods on doctype / text nodes
Confuse Document-level and Element-level first children
Assign to firstElementChild (read-only)
Use it instead of querySelector for deep lookups
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.firstElementChild
First Element under Document — usually <html>.
5
Core concepts
📄01
Returns
Element | null
API
✓02
Status
baseline
Standard
🌐03
HTML
<html>
Usually
🚫04
Skips
doctype / text
Safe
🔗05
Twin
documentElement
Same root
❓ Frequently Asked Questions
The document's first child Element, or null if there are no child elements. For HTML documents this is usually the root <html> element (MDN).
No. MDN marks Document.firstElementChild as Baseline Widely available (since April 2018). It is a standard ParentNode property on Document.
firstChild can return any node type — often the DocumentType (doctype) or a text/comment node. firstElementChild skips non-element children and returns the first Element only.
On normal HTML pages, yes — both typically refer to the <html> element. Prefer document.documentElement when you specifically mean the document root; firstElementChild is the ParentNode "first element child" API.
Yes. Element.firstElementChild returns the first element child of that element (for example the first <li> inside a <ul>). Document.firstElementChild is the same idea at the document level.
When the document has no child elements. Typical HTML pages always have at least <html>, so you usually get an Element.
Did you know?
firstElementChild, lastElementChild, children, and childElementCount all come from the same ParentNode mixin—so Document, Element, and DocumentFragment share the same “element children” vocabulary.