Home jQuery Traversing .last() jQuery .last() Method Definition
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().
$("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) Approach
How to Use .last() Build a set, then keep only the element at the final index.
Select the elements you care about (li, .card, and so on). Call .last() — no arguments. Work with the one-element (or empty) result. Chain after .find() / .filter() when the set is built in steps. Piece Required? Role Starting set Yes Collection to reduce .last()Yes Keeps the final match only Arguments None Always the last match
Pseudocode set = current jQuery collection
if set.length == 0:
return empty jQuery
return jQuery(set[set.length - 1]) // new one-element set Reference
Cheat Sheet Keep these facts handy while taking the last match.
Topic Detail Syntax .last()Index Final index in the set (length - 1) vs .eq(-1) Same result vs .first() Last vs first in the set vs :last Prefer method (selector deprecated 3.4) Empty set Returns empty jQuery (no error) Since jQuery 1.4
Try it
Live Preview Compare .last(), .eq(-1), and .first() on a short list.
Code
jQuery Examples Short, copy-ready snippets. Open a browser console with jQuery loaded, or use Try It Yourself for a live editor.
1. Official last 2. vs .eq(-1) 3. vs .first() 4. After .find() 5. Empty set Example 1 — Official Demo: $("ul li").last() Take the last list item in document order (jQuery API example).
var $last = $("ul li").last();
console.log($last.length);
console.log($last.text().trim());
// Official: $( "ul li" ).last().addClass( "highlight" ); 1
This is another note about it. How It Works Match all items. $("ul li") builds the full set.Keep the end. .last() drops every earlier item.One-element set. Length is 1.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.
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]); list item 5
list item 5
same node: true How It Works .last(). Shorthand for the end of the set..eq(-1). Same position, written as a negative index.Same DOM node. $a[0] === $b[0].See also. .eq() for any index.Example 3 — .last() vs .first() Opposite ends of the same matched set.
console.log("first:", $("li").first().text().trim());
console.log("last:", $("li").last().text().trim());
console.log("total:", $("li").length); first: list item 1
last: list item 5
total: 5 How It Works First end. Index 0 → list item 1.Last end. Final index → list item 5.Same set size. Total remains 5.Pair them. See .first() for the other end.Example 4 — Chain After .find() Build a descendant set, then take its last match.
var $hit = $("#menu").find("li").last();
console.log($hit.text().trim());
console.log("from find count:", $("#menu").find("li").length); Contact
from find count: 3 How It Works Scope. $("#menu") is the root.Find items. .find("li") collects three links.Take last. .last() keeps “Contact”.Common pattern. Narrow, then pick the trailing item.Example 5 — Empty Set Is Safe Calling .last() on zero matches returns an empty jQuery object.
var $hit = $(".missing").last();
console.log("length:", $hit.length);
console.log("text:", JSON.stringify($hit.text())); How It Works No matches. $(".missing") is empty..last() stays empty. Length remains 0.No throw. Safe to chain further methods.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.
Compatibility
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.
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.
Analysis
Performance Notes Approach Cost Best for $("#id li").last()Scoped query + O(1) pick Typical UI work $("li").last()Full page query first Small 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().
Remember
Key Takeaways One line: .last() means “keep the final item of this set” — scope the set, then take the trailing match.
Frequently Asked Questions What does jQuery .last() do? .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.
Does .last() take arguments? No. It accepts no parameters. For other positions, use .eq(n). For the first element, use .first() or .eq(0).
How is .last() different from .eq(-1)? They return the same element. .last() is a clear shorthand for the end of the set; .eq(-1) makes the negative index explicit.
How is .last() different from .first()? .last() picks the final element in the set. .first() picks index 0. They are complementary ends of the collection.
What if the set is empty? .last() returns an empty jQuery object (length 0). No error is thrown — further chain methods simply do nothing.
Should I use .last() or :last? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion