JavaScript Document lastElementChild Property

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

What You’ll Learn

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

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.

Related Document tutorials: firstElementChild, documentElement, children, Document constructor.

Understanding Document.lastElementChild

A read-only instance property from the ParentNode interface, available on Document and Element.

  • Value — last child Element, or null if none (MDN).
  • Skips — non-element nodes (doctype, text, comments) when finding the last element.
  • HTML typical case — root <html> (MDN).
  • Element twinelement.lastElementChild for nested trees.
  • Not settable — read-only; use DOM methods to change the tree.

📝 Syntax

JavaScript
document.lastElementChild

Value

The root <html> element on typical HTML documents (MDN), or null when there are no element children.

MDN example

JavaScript
document.lastElementChild;
// returns the root <html> element, the only child of the document

⚡ Quick Reference

GoalCode / note
Last element under documentdocument.lastElementChild
Root html (preferred name)document.documentElement
Tag name checkdocument.lastElementChild.tagName"HTML"
Same as documentElement?=== document.documentElement
Last child of a listul.lastElementChild
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.lastElementChild.

Type
Element | null

Read-only

HTML
<html>

Usually

Skips
non-elements

Doctype etc.

Status
baseline

Standard API

📋 lastChild vs lastElementChild

document.lastChilddocument.lastElementChild
Node kindsAny (element, text, comment, doctype)Elements only
Typical HTML pageOften <html> (last node)<html>
Safe for .tagName?Only when node is an ElementYes when not null
Beginner tipCheck nodeType firstPrefer for element work

Examples Gallery

Examples follow MDN Document: lastElementChild. Use View Output or Try It Yourself for each case.

📚 Getting Started

Get the document’s last element child and compare sibling APIs.

Example 1 — MDN: Root <html> Element

On a normal HTML page, the last element child is usually <html>.

JavaScript
const root = document.lastElementChild;
console.log(root.tagName); // "HTML"
console.log(root === document.documentElement); // true
Try It Yourself

How It Works

MDN: returns the root <html> element — the only element child of the document on typical HTML pages.

Example 2 — lastChild vs lastElementChild

Compare the last node of any type with the last element only.

JavaScript
const anyNode = document.lastChild;
const lastEl = document.lastElementChild;

console.log("lastChild nodeType:", anyNode && anyNode.nodeType);
console.log("lastChild tagName?:", anyNode && anyNode.tagName);
console.log("lastElementChild:", lastEl && lastEl.tagName);
Try It Yourself

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);
Try It Yourself

How It Works

When the document has multiple element children (unusual), first and last differ. HTML pages usually have just one.

Example 4 — Same Object as documentElement?

Confirm the last element child is the document root.

JavaScript
console.log(
  document.lastElementChild === document.documentElement
); // true on typical HTML documents

console.log(document.documentElement.lang || "(no lang)");
Try It Yourself

How It Works

Prefer document.documentElement in app code when your intent is “the document root.”

Example 5 — Element.lastElementChild (Last <li>)

MDN: use Element.lastElementChild for children inside the tree.

JavaScript
const list = document.querySelector("ul");
const lastItem = list.lastElementChild;

console.log(lastItem.textContent); // last <li> text
Try It Yourself

How It Works

Same ParentNode API at document level and element level — only the starting node changes.

🚀 Common Use Cases

  • Document root access — reach <html> without a selector (usually same as documentElement).
  • Tree walking — pair with firstElementChild / nextElementSibling.
  • Lists & menusul.lastElementChild for the last item.
  • Layout scripts — append to the last element child of a container.
  • Null-safe libraries — handle empty documents that return null.
  • Learning ParentNode — same API on Document and Element nodes.

🧠 How lastElementChild Is Chosen

1

Document has child nodes

Doctype, comments, and the <html> element may all exist.

Tree
2

Browser scans from the end

Skips non-element nodes until it finds the last Element child.

Filter
3

You read lastElementChild

Returns that Element or null (MDN).

Result
4

Typical HTML: <html>

Usually matches document.documentElement and often firstElementChild too.

📝 Notes

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE
Full support
Document.lastElementChild Excellent

Bottom line: Use lastElementChild when you need the last element child. On HTML pages it is usually document.documentElement.

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.

Continue with lastModified, firstElementChild, documentElement, or the JavaScript hub.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about document.lastElementChild

Last element child at the document level.

5
Core concepts
🌐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.

Next: lastModified

Learn when the current document was last modified.

lastModified →

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.

6 people found this page helpful