Start empty. A new div has no element children → null.
Compare children[…]. That index is undefined, not null.
Add only text. Text nodes do not count as element children.
Always null-check before reading tagName or textContent.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
lastElementChildExcellent
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.