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
Fundamentals
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).
Concept
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.
Foundation
📝 Syntax
JavaScript
const last = node.lastChild;
if (last) {
console.log(last.nodeName);
}
Return value
A Node, or null if there are no children.
Compare
⚖️ lastChild vs lastElementChild
node.lastChild
element.lastElementChild
Interface
Node
Element
Returns
Any last child node (or null)
Last element child (or null)
Whitespace
Often #text
Skips text / comments
Best for
Full DOM / text-aware code
Most UI element work
Cheat Sheet
⚡ Quick Reference
Goal
Code
Get last child
node.lastChild
Safe read
node.lastChild?.nodeName
Last element only
element.lastElementChild
Same as last index
node.childNodes[node.childNodes.length - 1] (or undefined if empty)
Clear from the end
while (node.lastChild) node.removeChild(node.lastChild);
MDN status
Baseline Widely available (since March 2016)
Snapshot
🔍 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
Hands-On
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.
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.
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 & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
lastChildExcellent
Bottom line: Read lastChild for any last node type; use lastElementChild when you only want an element.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about lastChild
Last child node—or null.
5
Core concepts
👇01
Last child
or null
API
✨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.