JavaScript Node lastChild Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Read-only

What You’ll Learn

The Node.lastChild property returns a node’s last child, or null if there are none. Learn the trailing whitespace #text gotcha, lastChild vs lastElementChild, and five practical examples with try-it labs.

01

Kind

Read-only property

02

Returns

Node or null

03

May be

#text / comment

04

Prefer

lastElementChild

05

Empty

null

06

Status

Baseline widely

Introduction

lastChild is a shortcut for “give me the last entry in childNodes.” Pair it with firstChild when you walk from either end of a parent’s children.

The surprise for beginners: pretty-printed HTML inserts whitespace text nodes after the last tag, so lastChild is often #text instead of the last element you see.

💡
Beginner tip

For UI work, prefer Element.lastElementChild. Use lastChild when you truly need any node type (including text).

Understanding lastChild

MDN: the read-only lastChild property returns the node’s last child in the tree, or null if the node has no children. It may be a Text or Comment node—not only an element.

  • Can be an element, text node, comment, or other node type.
  • Trailing whitespace between tags creates #text nodes.
  • Same idea as the last item in childNodes, or null when length is 0.
  • Read-only — you do not assign to lastChild.

📝 Syntax

JavaScript
const last = node.lastChild;
if (last) {
  console.log(last.nodeName);
}

Return value

A Node, or null if there are no children.

⚖️ lastChild vs lastElementChild

node.lastChildelement.lastElementChild
InterfaceNodeElement
ReturnsAny last child node (or null)Last element child (or null)
WhitespaceOften #textSkips text / comments
Best forFull DOM / text-aware codeMost UI element work

⚡ Quick Reference

GoalCode
Get last childnode.lastChild
Safe readnode.lastChild?.nodeName
Last element onlyelement.lastElementChild
Same as last indexnode.childNodes[node.childNodes.length - 1] (or undefined if empty)
Clear from the endwhile (node.lastChild) node.removeChild(node.lastChild);
MDN statusBaseline Widely available (since March 2016)

🔍 At a Glance

Four facts to remember about Node.lastChild.

Returns
Node | null

Last child

Baseline
widely

Since March 2016

Access
read-only

No assignment

Watch for
#text

Trailing space

Examples Gallery

Examples follow MDN Node.lastChild patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

See how trailing source whitespace changes lastChild.

Example 1 — Trailing Whitespace Makes #text

Pretty markup: newline/indent after the span, before the closing parent tag.

JavaScript
// <p id="para-01">
//   <span>Last span</span>
// </p>

const p01 = document.getElementById("para-01");
console.log(p01.lastChild.nodeName); // "#text"
Try It Yourself

How It Works

Any whitespace between the closing </span> and </p> becomes a text node, so that text node is last.

Example 2 — Compact Markup → Element

Remove the trailing whitespace and the span becomes the last child.

JavaScript
// <p id="para-01"><span>Last span</span></p>

const p01 = document.getElementById("para-01");
console.log(p01.lastChild.nodeName); // "SPAN"
Try It Yourself

How It Works

With no text after the span, the last child is the element node you intended.

📈 lastElementChild, null & Tables

Skip text nodes, handle empty parents, follow MDN’s table-row idea.

Example 3 — lastChild vs lastElementChild

Skip trailing whitespace by asking for the last element child.

JavaScript
const p = document.getElementById("para-01");
console.log(p.lastChild.nodeName);          // often "#text"
console.log(p.lastElementChild.nodeName);   // "SPAN"
Try It Yourself

How It Works

lastElementChild ignores text and comment nodes and returns the last element, or null if none exist.

Example 4 — Empty Node Returns null

Always guard before reading properties on lastChild.

JavaScript
const empty = document.createElement("div");
console.log(empty.lastChild); // null

if (empty.lastChild) {
  console.log(empty.lastChild.nodeName);
} else {
  console.log("No children");
}
Try It Yourself

How It Works

Reading .nodeName on null throws. Check the result (or use optional chaining) first.

Example 5 — Table Row lastChild (MDN-style)

MDN uses a table row: the last cell is often what you want—watch for text nodes if the markup is indented.

JavaScript
// <tr id="row1"><td>A</td><td>B</td><td>C</td></tr>

const tr = document.getElementById("row1");
const cornerTd = tr.lastChild;

console.log(cornerTd.nodeName);      // "TD"
console.log(cornerTd.textContent);   // "C"
Try It Yourself

How It Works

Compact cells make lastChild the last td. With pretty-printed rows, prefer lastElementChild so you do not land on trailing #text.

🚀 Common Use Cases

  • Grab the last cell / item — tables, lists, and toolbars (MDN-style).
  • Inspect trailing text — read a text node after the last tag.
  • Teach the DOM — show why indented HTML creates ending #text.
  • Prefer elements for UI — switch to lastElementChild for tags.
  • Clear from the endwhile (el.lastChild) el.removeChild(el.lastChild).

🧠 How lastChild Works

1

Look at childNodes

The engine considers the parent’s direct children in order.

Tree
2

Pick the last index

Return that node, whatever its type (element, text, …).

Last
3

Or return null

If there are no children, lastChild is null.

Empty
4

Your script branches

Check for null, then read nodeName / nodeType.

📝 Notes

  • Baseline Widely available (MDN, since March 2016).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Whitespace and comments can be the last child.
  • Use lastElementChild when you only want elements.
  • Related: firstChild, nextSibling, childNodes, isConnected, JavaScript hub.

Universal Browser Support

Node.lastChild is Baseline Widely available across modern browsers (MDN: since March 2016). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Node.lastChild

Safe for production. Remember trailing whitespace text nodes, and prefer lastElementChild for element-only UI code.

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 & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Long-standing support in legacy IE
Full support
lastChild Excellent

Bottom line: Read lastChild for any last node type; use lastElementChild when you only want an element.

Conclusion

Node.lastChild returns the last child node or null. Watch for trailing whitespace #text nodes in indented HTML, prefer lastElementChild for elements, and use it with firstChild when you work from either end.

Continue with nextSibling, firstChild, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Null-check lastChild before using it
  • Use lastElementChild for element-only needs
  • Expect trailing #text in pretty-printed HTML
  • Pair with firstChild when teaching both ends
  • Compare with the last childNodes index when debugging

❌ Don’t

  • Assume lastChild is always an element
  • Read .nodeName on a possible null
  • Assign to lastChild (read-only)
  • Ignore comments that might trail the child list
  • Confuse DOM Node with the Node.js runtime

Key Takeaways

Knowledge Unlocked

Five things to remember about lastChild

Last child node—or null.

5
Core concepts
02

#text trap

trailing space

Gotcha
⚖️ 03

vs element

lastElementChild

Compare
🔒 04

Null-safe

check first

Safety
📊 05

Tables / lists

last cell / item

Pattern

❓ Frequently Asked Questions

The node’s last child in the tree, or null if there are no children. The child can be an Element, Text, Comment, or another node type — not only an HTML element.
No. MDN marks Node.lastChild as Baseline Widely available (since March 2016). It is a standard DOM property — not Deprecated, Experimental, or Non-standard.
Whitespace after the last tag inside a parent (spaces, newlines, tabs) becomes a Text node. So lastChild is often #text even when the last visible tag is a span or td.
Use Element.lastElementChild when you want the last element child and want to skip text and comment nodes. Prefer it for most UI work.
lastChild is null. Always check before reading nodeName or other properties on the result.
firstChild is the first entry in childNodes; lastChild is the last. Pretty-printed HTML often has #text at both ends.
Did you know?

MDN highlights that lastChild can be Text or Comment—so pretty-printed parents often end with #text. That is why lastElementChild exists for element-only access at the end of the child list.

Next: nextSibling

Read the next sibling node—or null at the end of the list.

nextSibling →

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.

5 people found this page helpful