JavaScript Element lastElementChild Property

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

What You’ll Learn

Element.lastElementChild is a read-only instance property that returns the last child element, or null when none exist. Learn how it differs from lastChild, 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[last]

Introduction

When you need the last nested tag inside a parent—not a trailing text node from whitespace— lastElementChild is the direct read.

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

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

Need the very last node including text? Use lastChild. Need only the last tag? Use lastElementChild.

Understanding the Property

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

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

📝 Syntax

JavaScript
lastElementChild

Value

An Element reference for the last 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
RelatedfirstElementChild, children, lastChild
⚠️
Null safety

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

📋 MDN List Example Shape

MDN reads the last 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.lastElementChild.textContent);
// Third (3)

Related learning: children, firstElementChild, and lastChild.

⚡ Quick Reference

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

🔍 At a Glance

Four facts about Element.lastElementChild.

Kind
get only

Instance

Type
Element|null

Or null

Skips
text

And comments

Baseline
widely

Jul 2015+

Examples Gallery

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

📚 Getting Started

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

Example 1 — MDN Read textContent

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

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

How It Works

lastElementChild points at the last <li>, skipping any trailing text nodes. textContent returns its inner text.

📈 Compare, Null & Equivalence

See the lastChild difference, null cases, and children[last] parity.

Example 2 — vs lastChild

Trailing whitespace text nodes appear in lastChild but not in lastElementChild.

JavaScript
const box = document.getElementById("box");
console.log({
  lastChildType: box.lastChild.nodeName,
  lastElementChildTag: box.lastElementChild.tagName
});
Try It Yourself

How It Works

Pretty-printed HTML often puts a text node after the last tag. Prefer lastElementChild when you need the final element, not trailing 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.lastElementChild); // null
console.log(empty.lastChild !== null); // may still have a text node
Try It Yourself

How It Works

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

Example 4 — Equals children[children.length - 1]

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

JavaScript
const box = document.getElementById("box");
const last = box.children[box.children.length - 1];
console.log(box.lastElementChild === last); // true
console.log(box.lastElementChild.tagName); // P
Try It Yourself

How It Works

lastElementChild is a convenience read. When no elements exist, children[children.length - 1] is undefined while lastElementChild is null.

Example 5 — Support Snapshot

Feature-detect and remember the null-check tip.

JavaScript
console.log({
  supported: "lastElementChild" 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 last row, list item, or tab panel element.
  • Skipping trailing whitespace text nodes that lastChild would return.
  • Empty-state checks when no element children exist (null).
  • Quick access instead of children[children.length - 1] with clearer intent.
  • Pairing with firstElementChild 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 from the end backward.

Filter
3

Returns last Element or null

Same node as children[children.length - 1] when present.

Read
4

Re-read after DOM changes

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

📝 Notes

Browser Support

Element.lastElementChild 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.lastElementChild

Read-only — returns the last 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
lastElementChild Baseline

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

Conclusion

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

Continue with localName, firstElementChild, children, or the JavaScript hub.

💡 Best Practices

✅ Do

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

❌ Don’t

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

Key Takeaways

Knowledge Unlocked

Five things to remember about lastElementChild

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

5
Core concepts
📝 02

Element|null

not text

Type
🔍 03

Last tag

lists & rows

Use
04

Baseline

widely available

Status
🎯 05

Null check

empty parents

Tip

❓ Frequently Asked Questions

It returns the last child element of the element, 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.
An Element reference, or null when the parent has no element children (including when it only has text or comment nodes).
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.
It comes from the ParentNode mixin, so Document and DocumentFragment also expose lastElementChild.
Did you know?

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

Next: localName

Learn how Element.localName reads the local part of a tag name.

localName →

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