jQuery .last() Method

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

What Is .last()?

.last() reduces the current matched set to the final element and returns a new jQuery object. No arguments. Available since jQuery 1.4.

Prefer over :last

The :last selector is a jQuery extension deprecated in 3.4. Select with CSS, then call .last().

Remember
$("li").last()        → final list item
$("ul li").last()     → last li in any ul (document order)

set:  [0] [1] [2] [3] [4]
                         ↑
                      .last()

.last()   ===  .eq(-1)
.first()  ===  .eq(0)
NeedPrefer
Last in the set$("sel").last()
Nth by index$("sel").eq(n)
First in the set$("sel").first()

How to Use .last()

Build a set, then keep only the element at the final index.

  1. Select the elements you care about (li, .card, and so on).
  2. Call .last() — no arguments.
  3. Work with the one-element (or empty) result.
  4. Chain after .find() / .filter() when the set is built in steps.
PieceRequired?Role
Starting setYesCollection to reduce
.last()YesKeeps the final match only
ArgumentsNoneAlways the last match

Pseudocode

Pseudocode
set = current jQuery collection

if set.length == 0:
    return empty jQuery

return jQuery(set[set.length - 1])   // new one-element set

Cheat Sheet

Keep these facts handy while taking the last match.

TopicDetail
Syntax.last()
IndexFinal index in the set (length - 1)
vs .eq(-1)Same result
vs .first()Last vs first in the set
vs :lastPrefer method (selector deprecated 3.4)
Empty setReturns empty jQuery (no error)
SincejQuery 1.4

Live Preview

Compare .last(), .eq(-1), and .first() on a short list.

Official idea: highlight the last list item in document order.

Result
$("li").last()
matched: 1
text: This is another note about it.

/* final index in the set */

jQuery Examples

Short, copy-ready snippets. Open a browser console with jQuery loaded, or use Try It Yourself for a live editor.

Example 1 — Official Demo: $("ul li").last()

Take the last list item in document order (jQuery API example).

jQuery
var $last = $("ul li").last();
console.log($last.length);
console.log($last.text().trim());

// Official: $( "ul li" ).last().addClass( "highlight" );
Try It Yourself

How It Works

  1. Match all items. $("ul li") builds the full set.
  2. Keep the end. .last() drops every earlier item.
  3. One-element set. Length is 1.
  4. Official demo. Adds a highlight class to that item.

Example 2 — Same as .eq(-1)

Both methods return the last element as a jQuery object.

jQuery
var $a = $("li").last();
var $b = $("li").eq(-1);

console.log($a.text().trim());
console.log($b.text().trim());
console.log("same node:", $a[0] === $b[0]);
Try It Yourself

How It Works

  1. .last(). Shorthand for the end of the set.
  2. .eq(-1). Same position, written as a negative index.
  3. Same DOM node. $a[0] === $b[0].
  4. See also. .eq() for any index.

Example 3 — .last() vs .first()

Opposite ends of the same matched set.

jQuery
console.log("first:", $("li").first().text().trim());
console.log("last:", $("li").last().text().trim());
console.log("total:", $("li").length);
Try It Yourself

How It Works

  1. First end. Index 0 → list item 1.
  2. Last end. Final index → list item 5.
  3. Same set size. Total remains 5.
  4. Pair them. See .first() for the other end.

Example 4 — Chain After .find()

Build a descendant set, then take its last match.

jQuery
var $hit = $("#menu").find("li").last();
console.log($hit.text().trim());
console.log("from find count:", $("#menu").find("li").length);
Try It Yourself

How It Works

  1. Scope. $("#menu") is the root.
  2. Find items. .find("li") collects three links.
  3. Take last. .last() keeps “Contact”.
  4. Common pattern. Narrow, then pick the trailing item.

Example 5 — Empty Set Is Safe

Calling .last() on zero matches returns an empty jQuery object.

jQuery
var $hit = $(".missing").last();
console.log("length:", $hit.length);
console.log("text:", JSON.stringify($hit.text()));
Try It Yourself

How It Works

  1. No matches. $(".missing") is empty.
  2. .last() stays empty. Length remains 0.
  3. No throw. Safe to chain further methods.
  4. Guard when needed. Check .length before assuming a node exists.

Edge Cases & Pitfalls

Check these before relying on .last() in real projects.

order

Document / set order

“Last” means the final index in the current jQuery set — usually document order, not visual rightmost after CSS.

deprecated

Prefer over :last

:last is deprecated in jQuery 3.4. Use a CSS query, then .last().

empty

Empty set is silent

No match → empty result. Check .length before reading attributes or values.

scope

Scope the set first

$("li").last() is the last li on the whole page. Prefer $("#nav li").last() when you mean one list.

Browser Support

.last() is a core jQuery traversing method available since 1.4. It works wherever jQuery runs.

✓ jQuery 1.4+ · Core API

jQuery .last()

Works in all browsers with jQuery. Prefer .last() over the deprecated :last selector.

100% jQuery environments
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() Core method

Bottom line: Safe to use for taking the final match; pair with .first() or .eq(n) when you need other positions.

Performance Notes

ApproachCostBest for
$("#id li").last()Scoped query + O(1) pickTypical UI work
$("li").last()Full page query firstSmall pages only
$("li:last")Extension path (deprecated)Avoid in new code

.last() itself is cheap. Cost comes from building a large set first — scope with an id or class, then call .last().

Key Takeaways

  • Role: keep only the final element in the set.
  • Same as: .eq(-1).
  • Opposite: .first() for the first element.
  • Prefer method: over deprecated :last.

One line: .last() means “keep the final item of this set” — scope the set, then take the trailing match.

Frequently Asked Questions

.last() reduces the current jQuery set to the final element and returns a new jQuery object with that single match — or an empty set if there were no elements. Available since jQuery 1.4.
No. It accepts no parameters. For other positions, use .eq(n). For the first element, use .first() or .eq(0).
They return the same element. .last() is a clear shorthand for the end of the set; .eq(-1) makes the negative index explicit.
.last() picks the final element in the set. .first() picks index 0. They are complementary ends of the collection.
.last() returns an empty jQuery object (length 0). No error is thrown — further chain methods simply do nothing.
Prefer .last(). The :last selector is a jQuery extension deprecated in 3.4. Select with CSS first, then call .last().

Next: .has() Method

Keep elements that contain a matching descendant.

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

11 people found this page helpful