JavaScript Element lastElementChild Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

Element.lastElementChild is a read-only property that returns the parent’s last 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.lastElementChild. Start with this line.

JavaScript
element.lastElementChild
CallWhat it does
element.lastElementChildPrimary signature.

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

Library declarations
JavaScript
element.lastElementChild

Parameters

ParameterDescription
Parent nodeRequired. Container that may have element children
childrenOptional. Live list — last item matches when present

Return Value

Return ValueDescription
*Element or null

JavaScript Examples

Example 1 — Read the Last Child Element

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

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

console.log(list.lastElementChild.textContent);
// "Third (3)"
Try It Yourself

How It Works

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

Example 2 — lastElementChild vs lastChild

Whitespace after a child element makes lastChild a text node.

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

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

How It Works

  1. Pretty HTML adds text. A trailing newline becomes a text node.
  2. lastChild sees that text. nodeName is "#text".
  3. lastElementChild skips it. You get the <span>.
  4. Rule of thumb. Want an element? Prefer lastElementChild.

Example 3 — Empty Parent Returns null

No element children → null (and the last children slot is undefined).

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

console.log(empty.lastElementChild);              // null
console.log(empty.children[empty.children.length - 1]); // undefined

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

How It Works

  1. Start empty. A new div has no element children → null.
  2. Compare children[…]. That index 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.lastElementChild is Baseline Widely available (MDN: since July 2015). Safe across modern and older evergreen browsers.

✓ Baseline · Widely available

Element.lastElementChild

Read-only last 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
lastElementChild Excellent

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

Frequently Asked Questions

The last child Element of the parent, or null if there is no element child. Text and comment nodes are skipped.
No. MDN marks Element.lastElementChild as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
lastChild returns the last node of any type (element, text, comment). lastElementChild returns only the last Element child, skipping text and comments.
When element children exist, yes — lastElementChild equals the final item in children. When there are no element children, lastElementChild is null.
No. It is read-only. To change the last child element, use DOM methods such as append(), insertBefore(), or replaceChildren().
It comes from the ParentNode mixin, so Document and DocumentFragment also expose lastElementChild.

Next: localName

Learn how to read an element’s local name (tag name without namespace prefix).

localName 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