JavaScript Element firstElementChild Property

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

What You’ll Learn

Element.firstElementChild is a read-only instance property that returns the first child element, or null when none exist. Learn how it differs from firstChild, how it relates to children, and how to use it safely—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read-only

03

Type

Element or null

04

Skips

Text & comments

05

Status

Baseline widely

06

Same as

children[0]

Introduction

When you need the first nested tag inside a parent—not a text node from whitespace— firstElementChild is the direct read.

It is ideal for grabbing the first list item, table row, or card section without filtering childNodes yourself.

JavaScript
const list = document.getElementById("list");
console.log(list.firstElementChild.textContent);
💡
Beginner tip

Need the very first node including text? Use firstChild. Need only the first tag? Use firstElementChild.

Understanding the Property

MDN: the read-only firstElementChild property returns the element’s first child element, or null if there is no such child.

  • Read-only — re-read after DOM changes to get the current first element.
  • Element or null — never returns text or comment nodes.
  • Element-only view — same first item as children[0] when present.
  • Baseline — widely available since July 2015 (MDN).

📝 Syntax

JavaScript
firstElementChild

Value

An Element reference for the first child element, or null when the parent has no element children. Always check for null before reading properties like textContent.

ItemDetail
TypeElement or null
AccessRead-only property
SkipsText and comment nodes
RelatedlastElementChild, children, firstChild
⚠️
Null safety

An empty container or one with only text returns null. Use optional chaining (el.firstElementChild?.textContent) or an explicit check.

📋 MDN List Example Shape

MDN reads the first list item’s text from a ul:

JavaScript
<ul id="list">
  <li>First (1)</li>
  <li>Second (2)</li>
  <li>Third (3)</li>
</ul>
JavaScript
const list = document.getElementById("list");
console.log(list.firstElementChild.textContent);
// First (1)

Related learning: children, lastElementChild, and firstChild.

⚡ Quick Reference

GoalCode / note
First element childel.firstElementChild
Same when presentel.children[0]
First node (any type)el.firstChild
Last element childel.lastElementChild
Empty checkel.firstElementChild === null
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.firstElementChild.

Kind
get only

Instance

Type
Element|null

Or null

Skips
text

And comments

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: firstElementChild. Labs use simple lists and containers so you can compare element-only reads safely.

📚 Getting Started

Read the first element child like MDN’s list example.

Example 1 — MDN Read textContent

Log the first list item’s text from a ul.

JavaScript
const list = document.getElementById("list");
console.log(list.firstElementChild.textContent);
Try It Yourself

How It Works

firstElementChild points at the first <li>, skipping any text nodes. textContent returns its inner text.

📈 Compare, Null & Equivalence

See the firstChild difference, null cases, and children[0] parity.

Example 2 — vs firstChild

Whitespace text nodes appear in firstChild but not in firstElementChild.

JavaScript
const box = document.getElementById("box");
console.log({
  firstChildType: box.firstChild.nodeName,
  firstElementChildTag: box.firstElementChild.tagName
});
Try It Yourself

How It Works

Pretty-printed HTML often puts a text node first. Prefer firstElementChild when you need the first tag, not whitespace.

Example 3 — null When No Element Children

A parent with only text (or no children) returns null.

JavaScript
const empty = document.getElementById("empty");
console.log(empty.firstElementChild); // null
console.log(empty.firstChild !== null); // may still have a text node
Try It Yourself

How It Works

Text-only containers have a firstChild text node but no element children, so firstElementChild is null.

Example 4 — Equals children[0]

When element children exist, both accessors refer to the same node.

JavaScript
const box = document.getElementById("box");
console.log(box.firstElementChild === box.children[0]); // true
console.log(box.firstElementChild.tagName); // P
Try It Yourself

How It Works

firstElementChild is a convenience read. When no elements exist, children[0] is undefined while firstElementChild is null.

Example 5 — Support Snapshot

Feature-detect and remember the null-check tip.

JavaScript
console.log({
  supported: "firstElementChild" in Element.prototype,
  returns: "Element or null",
  tip: "Check for null before reading properties",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Safe to use in all modern browsers and IE. Always guard against null on empty containers.

🚀 Common Use Cases

  • Reading the first row, list item, or tab panel element.
  • Skipping whitespace text nodes that firstChild would return.
  • Empty-state checks when no element children exist (null).
  • Quick access instead of children[0] with clearer intent.
  • Pairing with lastElementChild for first/last element reads.

🔧 How It Works

1

Parent holds mixed children

Elements plus possible text or comments.

DOM
2

Browser scans child list

Skips non-element nodes in document order.

Filter
3

Returns first Element or null

Same node as children[0] when present.

Read
4

Re-read after DOM changes

Insert or remove children and the next read reflects the update.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • Returns null, not undefined, when no element children exist.
  • From the ParentNode mixin—also on Document and DocumentFragment.
  • Related: elementTiming, children, lastElementChild, id, JavaScript hub.

Browser Support

Element.firstElementChild is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.firstElementChild

Read-only — returns the first child Element or null (skips text/comment nodes).

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Supported (IE 9+)
Yes
firstElementChild Baseline

Bottom line: Use firstElementChild to read the first nested tag. Prefer it over firstChild when whitespace text nodes would get in the way. Always check for null on empty containers.

Conclusion

firstElementChild is the element-only shortcut to the first nested tag. It skips text and comments, returns null when no elements exist, and matches children[0] when they do.

Continue with id, children, elementTiming, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check for null before reading child properties
  • Prefer firstElementChild over firstChild for tags
  • Use optional chaining: el.firstElementChild?.textContent
  • Pair with lastElementChild for boundary reads
  • Re-read after DOM mutations that reorder children

❌ Don’t

  • Assume firstChild is always an element
  • Skip null checks on possibly empty containers
  • Assign el.firstElementChild = ... (read-only)
  • Confuse null with undefined from children[0]
  • Use it when you need every node type—use firstChild

Key Takeaways

Knowledge Unlocked

Five things to remember about firstElementChild

Read-only Element or null—first tag child only.

5
Core concepts
📝 02

Element|null

not text

Type
🔍 03

First tag

lists & rows

Use
04

Baseline

widely available

Status
🎯 05

Null check

empty parents

Tip

❓ Frequently Asked Questions

It returns the first child element of the element, or null if there is no element child. Text and comment nodes are skipped.
No. MDN marks Element.firstElementChild as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
An Element reference, or null when the parent has no element children (including when it only has text or comment nodes).
firstChild returns the first node of any type (element, text, comment). firstElementChild returns only the first Element child, skipping text and comments.
When element children exist, yes — firstElementChild equals children[0]. When there are no element children, firstElementChild is null and children[0] is undefined.
It comes from the ParentNode mixin, so Document and DocumentFragment also expose firstElementChild.
Did you know?

Like children, firstElementChild comes from the ParentNode mixin—so Document and DocumentFragment expose it too.

Next: id

Learn how Element.id gets and sets the HTML id attribute.

id →

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.

5 people found this page helpful