Document.lastElementChild is a read-only property that returns the document’s last child Element, or null if there are none (MDN). For HTML documents this is usually the only child—the root <html> element.
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax
Use document.lastElementChild. Start with this line.
JavaScript
document.lastElementChild
Call
What it does
document.lastElementChild
Primary signature.
Built into the browser. Read or assign document.lastElementChild — no import required.
Library declarations
JavaScript
document.lastElementChild
Parameters
Parameter
Description
lastElementChild
Required. Read-only Element or null
Null check
Optional. Needed only for unusual empty documents
Return Value
Return Value
Description
*
The current value of document.lastElementChild.
JavaScript Examples
Example 1 — Read document.lastElementChild
Log the last element child of the document (MDN: usually <html>).
JavaScript
const root = document.lastElementChild;
console.log(root.tagName);
// "HTML" on a normal HTML page
Element-only.lastElementChild returns node type 1 (ELEMENT_NODE) or null.
Any node.lastChild may be element, text (3), or comment (8).
Often equal here. On many pages the last child of document is still <html>.
Rule of thumb. Need an element? Use lastElementChild. Need any node? Use lastChild.
Compatibility
Browser Support
Document.lastElementChild is Baseline Widely available on MDN (since April 2018). It works across modern browsers as part of the ParentNode mixin.
✓ Baseline · Widely available
Document.lastElementChild
Read-only last child Element of the document — usually the root
element on HTML pages.
99%Browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
lastElementChildExcellent
Bottom line: Safe everywhere you teach DOM APIs. Prefer document.documentElement when you specifically need the HTML root. Use Element.lastElementChild for children inside a specific element.
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 be any node type — text, comment, or element. lastElementChild skips non-element children and returns only the last Element.
On normal HTML pages with a single root element, yes — both typically refer to <html>. 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.
When the document has no child elements. Typical HTML pages always have at least <html>, so you usually get an Element.