The read-only lastChild property returns the node’s last child in the tree, or null if there are none (MDN). That child may be an element, text, or comment — not only a tag.
Status: Baseline Widely available (since March 2016) — not Deprecated, Experimental, or Non-standard. Trailing whitespace often makes lastChild a #text node — prefer lastElementChild for the last element.
Remember
parent.lastChild
→ last Node in childNodes (or null)
Same idea: parent.childNodes[parent.childNodes.length - 1]
May be:
Element → "SPAN", "TD", …
Text → "#text" (common with indented HTML)
Comment → "#comment"
Need last element only?
parent.lastElementChild
Read it, null-check, then inspect nodeName / nodeType.
Get a parent node.
Read parent.lastChild.
If not null, use the node (or compare with lastElementChild).
For UI that needs a tag, prefer lastElementChild.
Piece
Required?
Role
parent
Yes
Node whose last child you want
Return
—
Node or null
Writable?
No
Read-only
Pseudocode
Pseudocode
child = parent.lastChild
if child is null:
parent has no children
else:
// may be #text because of trailing whitespace
name = child.nodeName
want a tag?
el = parent.lastElementChild
Reference
Cheat Sheet
Keep these facts handy when reading the last child.
Topic
Detail
Returns
Node or null
Equals
Last entry of childNodes when children exist
Whitespace
Often yields trailing #text
Elements only
lastElementChild
Empty parent
null — check first
Standard
Baseline Widely available (since March 2016)
Try it
Live Preview
Compare indented vs compact markup for the same visual content.
Samples
Pretty span
Compact span
Browser demo of Node.lastChild vs lastElementChild.
Result
Pick indented or compact, then Inspect…
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Same rules at the other end of childNodes; pretty HTML often has #text at both ends.
Compatibility
Browser Support
Node.lastChild is marked Baseline Widely available on MDN (since March 2016).
✓ Baseline · Widely available
Node.lastChild
Returns the last child Node or null. May be a text node from trailing whitespace. Prefer lastElementChild for the last element.
100%Widely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
lastChildExcellent
Bottom line: Safe everyday DOM API. Null-check, expect #text from pretty HTML, and use lastElementChild for tags. See the MDN lastChild reference.
Analysis
Performance Notes
Approach
Cost
Best for
lastChild
O(1)
Any last node type
lastElementChild
O(1)
Last element tag
childNodes[length - 1]
O(1)
Same as lastChild
Reading either property is cheap. Choose by meaning (any node vs element), not by speed.
Remember
Key Takeaways
Last child node — or null if empty.
Trailing whitespace: often #text after your last tag.
UI tags: use lastElementChild.
Null-check before using the result.
One line:lastChild is the last node in the child list — frequently trailing whitespace text in pretty HTML.
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 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.