JavaScript Element firstElementChild Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

Element.firstElementChild is a read-only property that returns the parent’s first child Element, or null if there are no child elements (MDN).

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use el.firstElementChild. Start with this line.

JavaScript
element.firstElementChild
CallWhat it does
element.firstElementChildPrimary signature.

Built into the browser. Read or assign el.firstElementChild — no import required.

Library declarations
JavaScript
element.firstElementChild

Parameters

ParameterDescription
Parent nodeRequired. Container that may have element children
childrenOptional. Live list of all element children

Return Value

Return ValueDescription
*Element or null

JavaScript Examples

Example 1 — Read the First Child Element

Log the text of the first <li> in a list (MDN shape).

JavaScript
const list = document.getElementById("list");

console.log(list.firstElementChild.textContent);
// "First (1)"
Try It Yourself

How It Works

  1. Find the parent. getElementById("list") returns the <ul>.
  2. Read firstElementChild. It is the first <li>.
  3. Read text. textContent is "First (1)".
  4. No null here. The list has element children, so the property is not null.

Example 2 — firstElementChild vs firstChild

Whitespace before a child element makes firstChild a text node.

JavaScript
const box = document.getElementById("box");
// <div id="box">\\n  <span>Hi</span>\\n</div>

console.log(box.firstChild.nodeName);           // "#text"
console.log(box.firstElementChild.tagName);     // "SPAN"
console.log(box.firstElementChild.textContent); // "Hi"
Try It Yourself

How It Works

  1. Pretty HTML adds text. Newlines/spaces become text nodes.
  2. firstChild sees that text. nodeName is "#text".
  3. firstElementChild skips it. You get the <span>.
  4. Rule of thumb. Want an element? Prefer firstElementChild.

Example 3 — Empty Parent Returns null

No element children → null (and children[0] is undefined).

JavaScript
const empty = document.createElement("div");

console.log(empty.firstElementChild);  // null
console.log(empty.children[0]);        // undefined

empty.textContent = "only text";
console.log(empty.firstElementChild);  // null
Try It Yourself

How It Works

  1. Start empty. A new div has no element children → null.
  2. Compare children[0]. That is undefined, not null.
  3. Add only text. Text nodes do not count as element children.
  4. Always null-check before reading tagName or textContent.

Browser Support

Element.firstElementChild is Baseline Widely available (MDN: since July 2015). Safe across modern and older evergreen browsers.

✓ Baseline · Widely available

Element.firstElementChild

Read-only first child Element or null — ParentNode mixin, skips text and comments.

99% Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
firstElementChild Excellent

Bottom line: Safe for production. Prefer it over firstChild whenever you specifically need an Element child.

Frequently Asked Questions

The first child Element of the parent, 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.
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.
No. It is read-only. To change the first child element, use DOM methods such as prepend(), insertBefore(), or replaceChildren().
It comes from the ParentNode mixin, so Document and DocumentFragment also expose firstElementChild.

Next: id

Learn how to get and set an element’s unique identifier string.

id tutorial →

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.

11 people found this page helpful