jQuery .last() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

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

Syntax

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

jQuery
$("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)
CallWhat it does
.last()Learn the jQuery .last() method: reduce a matched set to the final element, official list highlight demo, vs .eq(-1) and .first(), chain after .find(), examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Select the elements you care about (li, .card, and so on).
Step 2Call .last() — no arguments.
Step 3Work with the one-element (or empty) result.

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 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.

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].

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.

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

11 people found this page helpful