JavaScript Node lastChild Property

Beginner
5 min read
Updated: Oct 2026
5 examples
Live preview

What Is lastChild?

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
GoalPrefer
Any last child nodelastChild
Last element childlastElementChild
First child nodefirstChild

How to Use lastChild

Read it, null-check, then inspect nodeName / nodeType.

  1. Get a parent node.
  2. Read parent.lastChild.
  3. If not null, use the node (or compare with lastElementChild).
  4. For UI that needs a tag, prefer lastElementChild.
PieceRequired?Role
parentYesNode whose last child you want
Return—Node or null
Writable?NoRead-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

Cheat Sheet

Keep these facts handy when reading the last child.

TopicDetail
ReturnsNode or null
EqualsLast entry of childNodes when children exist
WhitespaceOften yields trailing #text
Elements onlylastElementChild
Empty parentnull — check first
StandardBaseline Widely available (since March 2016)

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…

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Read lastChild

Log the last child’s nodeName when it exists.

JavaScript
const box = document.getElementById("box");
const child = box.lastChild;

if (child) {
  console.log(child.nodeName);
} else {
  console.log("no children");
}
Try It Yourself

How It Works

  1. Select the parent. Any node that may have children.
  2. Read lastChild. Last entry of the child list.
  3. Null-check. Empty parents return null.
  4. Log nodeName. Shows the kind of node you got.

Example 2 — Trailing Whitespace → #text

Indented markup often inserts a text node after the last tag.

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

How It Works

  1. Pretty HTML has newlines. After </span> before </p>.
  2. Browser adds a Text node. That becomes lastChild.
  3. nodeName is #text. Not the span you see on screen.
  4. Compact HTML removes that trailing text node.

Example 3 — Prefer lastElementChild

Skip text/comments when you want the last tag (MDN tip).

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

How It Works

  1. Read lastChild. May be trailing whitespace text.
  2. Read lastElementChild. Skips non-elements.
  3. Compare names. #text vs SPAN.
  4. Use lastElementChild for everyday UI code.

Example 4 — Empty Parent → null

Always guard before touching properties on the result.

JavaScript
const empty = document.getElementById("empty");
console.log(empty.lastChild); // null
console.log(empty.hasChildNodes()); // false
Try It Yourself

How It Works

  1. Empty element. No child nodes at all.
  2. lastChild is null. Not an empty string.
  3. hasChildNodes is false. Same idea as a boolean check.
  4. Avoid .nodeName on null — that throws.

Example 5 — Remove the Last Child

Drop lastChild once when it exists.

JavaScript
const box = document.getElementById("box");
if (box.lastChild) {
  box.removeChild(box.lastChild);
}
console.log(box.lastChild && box.lastChild.nodeName);
Try It Yourself

How It Works

  1. Check lastChild. Skip if already empty.
  2. removeChild that node. Drops the current last child.
  3. List shrinks. A former sibling becomes lastChild.
  4. Often removes #text first on pretty HTML — use lastElementChild if you meant a tag.

Edge Cases & Pitfalls

Check these before treating lastChild as “the last tag.”

whitespace

Indented HTML → trailing #text

MDN: any node type can be last — spaces/newlines after the last tag often become lastChild.

null

No children → null

Always null-check before reading nodeName or calling methods on the result.

elements

Need a tag → lastElementChild

Use lastElementChild to skip text and comments.

pair

Mirror of firstChild

Same rules at the other end of childNodes; pretty HTML often has #text at both ends.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
lastChild Excellent

Bottom line: Safe everyday DOM API. Null-check, expect #text from pretty HTML, and use lastElementChild for tags. See the MDN lastChild reference.

Performance Notes

ApproachCostBest for
lastChildO(1)Any last node type
lastElementChildO(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.

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.

Next: nextSibling

Learn how to walk to the next sibling node in the tree.

nextSibling tutorial →

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.

8 people found this page helpful