jQuery :last Selector

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

What Is :last?

:last (since jQuery 1.0) selects exactly one element — the last in the matched set, in document order. It is a jQuery extension (not CSS) and is deprecated since 3.4. Prefer .last().

Remember
// Preferred (official pattern)
$("tr").last();

// Legacy selector (deprecated 3.4)
$("tr:last");

// Same idea as :eq(-1)
$("tr:eq(-1)");

// Not the same as :last-child (many per parent)
$("tr:last-child");
NeedUse
Last in a matched set$("tr").last()
Last child per parent$("tr:last-child")
Last of a tag type$("td:last-of-type")
Index from end$("tr").eq(-1)
First in a set$("tr").first()

How to Use .last() / :last

Select with CSS first, then take the final match from that set.

  1. Build a matched set with a pure CSS selector: $("tr") or $("ul.nav li").
  2. Chain .last() to keep only the final element.
  3. Use the result — .text(), .addClass(), events.
  4. Avoid putting :last inside the selector string in new code.
  5. Use :last-child when you need one match per parent.
PieceRequired?Role
CSS selectorYesBuilds the matched set
.last()PreferredKeeps only the final match
:lastLegacySame idea inside the selector (deprecated)

Pseudocode

Pseudocode
build a matched set with a CSS selector
    e.g. all tr elements in document order

keep only the final item in that set
    → result length is 0 or 1

.compare with :last-child:
    for each parent, keep the last child
    → result may contain many elements

Cheat Sheet

Keep these facts handy while picking the last matched element.

TopicDetail
Prefer$("elem").last()
Legacy$("elem:last") (deprecated 3.4)
SincejQuery 1.0
Matches0 or 1 element
Equivalent:eq(-1) / .eq(-1)
vs :last-childOne from set vs one per parent
CSS?No — jQuery extension
Official demo$("tr").last()

Live Preview

Mark the last row with .last(), compare with :last-child, or count matches.

Two lists below. .last() returns one li; :last-child can return two.

  • A1
  • A2
  • A3
  • B1
  • B2
Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Official $("tr").last()

Mark the last table row (official demo idea, CSS-free).

jQuery
$("tr").last().find("td").text(function (i, t) {
  return "[last] " + t;
});
Try It Yourself

How It Works

  1. Select. $("tr") builds the full row set.
  2. Filter. .last() keeps only the final row.
  3. Update. The last cell text is prefixed for a clear cue.
  4. Count. Result length is always 0 or 1.

Example 2 — .last() vs :last-child

Official distinction: one from the set vs one per parent.

jQuery
$("li").last().text(function (i, t) {
  return t + " [.last()]";
});
$("li:last-child").text(function (i, t) {
  return t + " (last-child)";
});

$("#out").text(
  ".last() → " + $("li").last().length +
  " | :last-child → " + $("li:last-child").length
);
Try It Yourself

How It Works

  1. .last(). Takes all li elements, keeps only the final one.
  2. :last-child. Matches the last li in each list.
  3. Count. Two lists → two last-children; .last() stays at 1.
  4. Pick. Use .last() for one item; :last-child for per-group UI.

Example 3 — Same as :eq(-1)

Both approaches return the same single element.

jQuery
$("#out").text(
  ".last() → " + $("li").last().text() +
  " | .eq(-1) → " + $("li").eq(-1).text()
);
Try It Yourself

How It Works

  1. Equivalence. :last equals :eq(-1) in the matched set.
  2. Modern. Prefer .last() or .eq(-1) over the selector form.
  3. Result. Both texts match for the same list.
  4. Choose. .last() reads clearer than .eq(-1).

Example 4 — Last Item in a Scoped List

Scope with a class so only one nav list is searched.

jQuery
$("ul.nav li").last().text(function (i, t) {
  return t + " ← last";
});
Try It Yourself

How It Works

  1. Scope. ul.nav li limits the matched set.
  2. Filter. .last() keeps the final nav item.
  3. Label. Text is appended so the match is easy to see.
  4. Note. Other lists on the page are not affected.

Example 5 — Prefer .last() Over :last

Same result — modern method vs deprecated selector.

jQuery
$("#out").text(
  ".last() → " + $("p").last().text() +
  " | :last → " + $("p:last").text()
);
Try It Yourself

How It Works

  1. Same set. Both pick the final p in document order.
  2. Method. $("p").last() is the recommended form.
  3. Selector. $("p:last") still works but is deprecated since 3.4.
  4. Habit. Write new code with .last() only.

Edge Cases & Pitfalls

Check these when the last match is not what you expected.

deprecated

Avoid :last in new selectors

Deprecated since jQuery 3.4. Prefer $("elem").last() after a CSS selector.

vs

Not the same as :last-child

.last() returns one element from the set. :last-child is about parent/child position and can match many.

scope

The set comes first

$("ul.nav li").last() and $("li").last() can differ — the CSS selector defines which elements compete for “last”.

perf

Not native CSS

:last cannot use querySelectorAll. Selecting with CSS first, then .last(), is faster on large pages.

Browser Support

:last is a jQuery extension (not standard CSS). Available since 1.0, deprecated in 3.4. Prefer .last(), which works in every browser that runs your loaded jQuery version.

✓ Deprecated · jQuery 1.0+

jQuery :last / .last()

The selector still works in jQuery 3.x but is deprecated. Use .last() for new code — same result, better practice.

1.0+ jQuery since
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
:last Deprecated 3.4

Bottom line: Safe to learn for reading old code. Write new code with $("elem").last() instead of $("elem:last").

Performance Notes

ApproachCostBest for
$("ul.nav li").last()CheapScoped lists (preferred)
$("li").last()Cheap–moderatePage-wide last item
$("li:last")ModerateLegacy only — avoid in new code

Pure CSS selection can use the browser engine; :last cannot. Chain .last() after CSS for the best of both.

Key Takeaways

  • Role: keep only the last element in a matched set (0 or 1).
  • Prefer: $("elem").last() over deprecated $("elem:last").
  • vs :last-child: one from the set vs one per parent.
  • Scope: the CSS selector decides which elements compete for “last”.

One line: use .last() to pick the final element in a jQuery set — not :last-child, and not the deprecated :last selector.

Frequently Asked Questions

:last selects exactly one element — the last in the matched set that precedes it, in document order. $("tr:last") matches only the last table row among all tr elements in scope. Available since jQuery 1.0.
Yes. Official jQuery documentation states that :last is equivalent to :eq(-1). Both return a single element at the final index of the matched set.
Yes — deprecated in jQuery 3.4. Official docs recommend removing :last from selectors and using the .last() method after a pure CSS selector instead: $("tr").last() rather than $("tr:last").
:last returns one element total from the matched set. :last-child can return many — one per parent. Official docs highlight this distinction explicitly.
No. :last is a jQuery extension and not part of the CSS specification. It does not work in native querySelectorAll. Use .last() on a CSS-selected collection for modern code.
Prefer .last(). Select with a CSS selector first, then chain .last(). That matches the official recommendation after the 3.4 deprecation and can use native querySelectorAll for the first step.

Next: Child selector

Select direct children with the parent > child combinator.

Child selector 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