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]
Fundamentals
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.
Concept
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).
Foundation
📝 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.
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
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)"
});
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).
BaselineWidely available
Google ChromeSupported
Yes
Microsoft EdgeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (IE 9+)
Yes
lastElementChildBaseline
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.
Wrap Up
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.
Use it when you need every node type—use lastChild
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about lastElementChild
Read-only Element or null—last tag child only.
5
Core concepts
📄01
Get only
on Element
Kind
📝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.