jQuery :last Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

: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().

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// 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");
CallWhat it does
CSS selectorBuilds the matched set
.last()Keeps only the final match
:lastSame idea inside the selector (deprecated)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Build a matched set with a pure CSS selector: $("tr") or $("ul.nav li").
Step 2Chain .last() to keep only the final element.
Step 3Use the result — .text(), .addClass(), events.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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: :has() Selector — Match Parents With Descendants

Select direct children with the parent > child combinator.

:has() Selector — Match Parents With Descendants tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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