JavaScript Document firstElementChild Property

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

What You’ll Learn

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

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.

Related Document tutorials: documentElement, doctype, body, Document constructor.

Understanding Document.firstElementChild

A read-only instance property from the ParentNode mixin on Document. It returns the first child that is an Element, or null (MDN).

  • ValueElement | null.
  • HTML pages — usually the root <html> (MDN).
  • Element-only — ignores doctype, text, and comment children.
  • Not assignable — you read it; you do not set a new first child via this property.
  • Same idea on elementsel.firstElementChild for nested trees.

📝 Syntax

JavaScript
document.firstElementChild

Value

An Element object, or null (MDN).

MDN example

JavaScript
document.firstElementChild;
// returns the root <html> element, the only child of the document

⚡ Quick Reference

GoalCode / note
First element under documentdocument.firstElementChild
Root html (preferred name)document.documentElement
Tag name checkdocument.firstElementChild.tagName"HTML"
Same object as documentElement?=== document.documentElement
First child of a listul.firstElementChild
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.firstElementChild.

Type
Element | null

Read-only

HTML
<html>

Usually

Skips
doctype

Non-elements

Status
baseline

Standard API

📋 firstChild vs firstElementChild

document.firstChilddocument.firstElementChild
Node kindsAny (doctype, text, comment, element)Elements only
Typical HTML pageOften DocumentType<html>
Safe for .tagName?Not alwaysYes when not null
Beginner tipEasy to confusePrefer for element work

Examples Gallery

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

📚 Getting Started

Get the document’s first element child and compare sibling APIs.

Example 1 — MDN: Root <html> Element

On a normal HTML page, the only element child of the document is <html>.

JavaScript
const root = document.firstElementChild;
console.log(root.tagName); // "HTML"
console.log(root === document.documentElement); // true
Try It Yourself

How It Works

MDN: returns the root <html> element, the only child of the document (for HTML).

Example 2 — firstChild vs firstElementChild

See why the element-only property is safer for beginners.

JavaScript
const anyNode = document.firstChild;
const firstEl = document.firstElementChild;

console.log("firstChild nodeType:", anyNode && anyNode.nodeType);
console.log("firstChild nodeName:", anyNode && anyNode.nodeName);
console.log("firstElementChild:", firstEl && firstEl.tagName);
// nodeType 10 = DOCUMENT_TYPE_NODE (doctype) on many pages
Try It Yourself

How It Works

Doctype nodeName can look like "html", but it is not an Element—use firstElementChild when you need .tagName / attributes.

📈 documentElement, Attributes & Nested Elements

Same object checks and the Element-level twin.

Example 3 — Same Object as documentElement?

On normal HTML pages both refer to the root element.

JavaScript
console.log(
  document.firstElementChild === document.documentElement
); // true on typical HTML documents

console.log(document.documentElement.lang || "(no lang)");
Try It Yourself

How It Works

Prefer document.documentElement in app code when your intent is “the document root.”

Example 4 — Read Root Attributes via firstElementChild

Theme / language attributes live on <html>.

JavaScript
const html = document.firstElementChild;
if (html) {
  console.log("tag:", html.tagName);
  console.log("lang:", html.getAttribute("lang"));
  console.log("class:", html.className);
}
Try It Yourself

How It Works

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

How It Works

Pretty-printed HTML often inserts text nodes; firstElementChild ignores them and returns the first real element.

🚀 Common Use Cases

  • Teaching ParentNode — show element-only child access at document level.
  • Avoiding doctype traps — skip firstChild when you need an Element.
  • Root inspection — read lang / classes (or use documentElement).
  • Generic helpers — code that works on Document or Element via ParentNode.
  • List / menu UIsel.firstElementChild for the first item.
  • Not a replacement for querySelector — use selectors when you need a specific descendant.

🧠 How Document Children Are Ordered

1

Doctype may come first

<!DOCTYPE html> becomes a DocumentType node under the Document.

firstChild
2

Then the root element

The <html> element is the first (and usually only) element child.

Element
3

firstElementChild skips non-elements

ParentNode walks children until it finds an Element (or returns null).

Filter
4

You get <html> (or null)

Ready for tagName, attributes, and further DOM walks.

📝 Notes

  • MDN: Baseline Widely available (since April 2018) — no Deprecated / Experimental / Non-standard banner.
  • For HTML documents, usually the only child element is <html> (MDN).
  • See Element.firstElementChild for children of specific elements (MDN).
  • Prefer document.documentElement when your intent is clearly “the root element.”
  • Related: documentElement, doctype, body.

Browser Support

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.

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 modern IE / Edge legacy paths
Full support
Document.firstElementChild Baseline 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.

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.

Continue with fonts, documentElement, doctype, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use firstElementChild when you need an Element
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about document.firstElementChild

First Element under Document — usually <html>.

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

Next: fonts

Learn document.fonts and the FontFaceSet CSS Font Loading API.

fonts →

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