Document.lastElementChild is a read-only instance property that returns the document’s last child Element, or null. For HTML this is usually the root <html> element. Learn how it differs from lastChild and documentElement, plus five examples with try-it labs.
01
Kind
Read-only
02
Returns
Element | null
03
HTML
Usually <html>
04
vs
lastChild
05
Related
firstElementChild
06
Status
Baseline widely
Fundamentals
Introduction
The Document node can have a doctype, comments, and element children. When you need the last element child only, use document.lastElementChild.
MDN: for HTML documents this is usually the only child element—the root <html>. For the last element inside a specific tag (like the last <li> in a list), use Element.lastElementChild instead.
💡
Pair with firstElementChild
On typical HTML pages, firstElementChild and lastElementChild both refer to the same <html> node because it is the document’s only element child.
lastChild nodeType: 1
lastChild tagName?: HTML
lastElementChild: HTML
How It Works
On many pages lastChild is also <html>, but lastElementChild guarantees an Element (or null).
📈 first vs last, documentElement & Nested Elements
Same-object checks and the Element-level twin.
Example 3 — firstElementChild vs lastElementChild
On HTML with one root element, both point to the same node.
JavaScript
console.log(
document.firstElementChild === document.lastElementChild
); // true when only one element child
console.log(document.lastElementChild.tagName);
Document.lastElementChild is marked Baseline Widely available on MDN (since April 2018). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Document.lastElementChild
Read-only last child Element of the document — usually the root html element.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in legacy IE
Full support
Document.lastElementChildExcellent
Bottom line: Use lastElementChild when you need the last element child. On HTML pages it is usually document.documentElement.
Wrap Up
Conclusion
Document.lastElementChild returns the last element child of the document — on HTML pages, that is almost always <html>. Pair it with firstElementChild, prefer document.documentElement for the root by name, and use Element.lastElementChild inside the tree.
Use lastElementChild when you need the last Element child
Null-check before reading .tagName or attributes
Prefer document.documentElement for the html root
Use element.lastElementChild for lists and containers
Compare with firstElementChild when debugging tree shape
❌ Don’t
Assume lastChild is always an Element
Confuse document-level and element-level lastElementChild
Expect multiple element children on typical HTML documents
Assign to lastElementChild — it is read-only
Skip null checks in library code for empty documents
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.lastElementChild
Last element child at the document level.
5
Core concepts
📄01
Returns
Element | null
API
🌐02
HTML
<html>
Usually
⚖️03
vs
lastChild
Elements only
🔀04
Pair
firstElementChild
Same on HTML
📝05
Nested
Element API
Last li
❓ Frequently Asked Questions
The document's last child Element, or null if there are no child elements. For HTML documents this is usually the root <html> element (MDN).
No. MDN marks Document.lastElementChild as Baseline Widely available (since April 2018). It is a standard ParentNode property on Document.
lastChild can return any node type — text, comment, or element. lastElementChild skips non-element children and returns the last Element only.
On normal HTML pages with a single root element, yes — both typically refer to the <html> element. Prefer document.documentElement when you specifically mean the document root.
On typical HTML pages both point to the same <html> element because it is the only element child of the document. firstElementChild is the first; lastElementChild is the last.
When the document has no child elements. Typical HTML pages always have at least <html>, so you usually get an Element.
Did you know?
lastElementChild and nextElementSibling / previousElementSibling are all part of the same ParentNode family — they skip text nodes and comments so you stay in “element land” while walking the DOM.