jQuery .last() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Final match

What You’ll Learn

The .last() traversing method reduces the current jQuery collection to the final element in the set. This tutorial covers the official list highlight demos, compares .last() with .eq(-1) and .first(), and shows how to chain after .filter() and .find().

01

Syntax

.last()

02

No args

Zero params

03

Final

Last in set

04

vs .eq(-1)

Same result

05

vs .first()

Opposite end

06

Since 1.4

Core API

Introduction

After selecting multiple elements — list items, table rows, pagination links — you often need just the last one. Maybe you want to style the bottom menu item, remove the final row, or focus the last input in a form. The .last() method picks that element in one readable step.

Available since jQuery 1.4 (added alongside .first()), .last() takes no arguments. It constructs a new jQuery object from the final element in the current set. It is shorthand for .eq(-1) — same result, clearer intent when you always want the trailing element.

Understanding the .last() Method

Given a jQuery object representing zero or more DOM elements, .last() returns a new jQuery object containing only the final element in that set. The index is relative to the current collection — not necessarily the last matching node in the entire document.

If the collection is empty, .last() returns an empty jQuery object. If the collection has one element, that element is returned unchanged in a new wrapper. The method does not search the DOM again — it only reads the current jQuery object.

💡
Beginner Tip

$("li").last() is equivalent to $("li").eq(-1). Use .last() when readability matters; use .eq(-1) when you are already mixing positive and negative indexes in the same chain.

📝 Syntax

General form of .last:

jQuery
.last()

Parameters

  • None — .last() does not accept any arguments.

Return value

  • A new jQuery object containing the last element in the current set, or an empty set if there are no matched elements.

Official jQuery API list example

jQuery
$( "li" ).last().css( "background-color", "red" );
// Red background on list item 5 only

⚡ Quick Reference

GoalCode
Last element in set$("li").last()
Same as eq(-1)$("li").eq(-1)
First element in set$("li").first() or .eq(0)
Last after filtering$("li").filter(".active").last()
Last descendant match$(".form").find("input").last()
Selector at query time$("li:last")

📋 .last() vs .eq(-1) vs .first() vs :last

Four ways to target the last element — readability, index, and timing.

.last()
final

Readable shorthand; no arguments

.eq(-1)
explicit

Same result; negative index form

.first()
index 0

Opposite end — first element only

:last
selector

Filter at initial query time

Examples Gallery

Examples 1–2 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery demos for last-element selection.

Example 1 — Official Demo: Red Background on Last List Item

Apply a red background to the final li in a simple list — the core official demo.

jQuery
$( "li" ).last().css( "background-color", "red" );
Try It Yourself

How It Works

$("li") matches all five items. .last() narrows to the final list item before .css() runs.

Example 2 — Official Demo: Highlight Last ul li

Add a yellow highlight class to the last list item inside a ul — official interactive demo pattern.

jQuery
$( "ul li" ).last().addClass( "highlight" );
Try It Yourself

How It Works

When multiple ul blocks exist, $("ul li") flattens all list items. .last() picks the very last li in document order.

📈 Practical Patterns

Equivalence with .eq(-1), comparison with .first(), and post-filter selection.

Example 3 — .last() vs .eq(-1)

Both methods target the same element — compare readability on identical markup.

jQuery
$( "#last-demo li" ).last().css( "color", "green" );
$( "#eq-demo li" ).eq( -1 ).css( "color", "green" );

// Identical result — different syntax
Try It Yourself

How It Works

Internally, .last() is implemented as a call to .eq(-1). Choose .last() for clarity; choose .eq(-1) when mixing with other index values in the same chain.

Example 4 — .last() vs .first() on the Same List

Style opposite ends of a collection — complementary jQuery 1.4 shortcuts.

jQuery
$( "#ends-demo li" ).first().css( "border-top", "3px solid blue" );
$( "#ends-demo li" ).last().css( "border-bottom", "3px solid red" );

// First and last items styled differently
Try It Yourself

How It Works

.first() and .last() were added together in jQuery 1.4 as readable bookends for any matched collection.

Example 5 — .last() After .filter()

Get the final active list item from a filtered subset — a common real-world pattern.

jQuery
$( "li" )
  .filter( ".active" )
  .last()
  .css( "font-weight", "bold" );
Try It Yourself

How It Works

.filter(".active") narrows to matching items. .last() then picks only the trailing active item — useful for breadcrumbs, tab trails, or “most recent” selection logic.

🚀 Common Use Cases

  • Style bottom list item$("ul.menu li").last().addClass("bottom").
  • Remove final row$("tbody tr").last().remove().
  • Focus last input$(".form").find("input").last().focus().
  • Append to last panel$(".panel").last().append(content).
  • Last visible tab$(".tab").filter(":visible").last().
  • Pagination next anchor$(".page-link").last().addClass("current").

🧠 How .last() Picks One Element

1

Start with collection

jQuery object holds zero or more elements.

Input
2

Read final index

Take the last element in the set.

Index
3

Wrap or empty

One-element jQuery object, or empty if none.

Result
4

Return & chain

New jQuery object — continue with .css(), .remove(), etc.

📝 Notes

  • Available since jQuery 1.4 — no arguments accepted.
  • Equivalent to .eq(-1) — shorthand for the final index only.
  • Operates on the current jQuery collection — does not re-query the DOM.
  • Empty input collection returns empty output — safe to chain.
  • Pushes onto jQuery’s internal stack — pair with .end() to restore the previous set.
  • For the first element, use .first() or .eq(0) instead.

Browser Support

.last() has been part of jQuery since 1.4+. It relies on collection indexing with no browser-specific behavior.

jQuery 1.4+

jQuery .last()

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: collection[collection.length - 1] wrapped back into jQuery.

100% With jQuery loaded
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() Universal

Bottom line: Safe in any jQuery project. Use .last() for readability; use .eq(-1) when index logic is already in play.

Conclusion

The jQuery .last() method reduces a matched collection to the single final element. It is the clearest way to target the trailing item when you do not need a custom index.

Remember the official list lesson: only the last item gets the red background or highlight class. Pair .last() with .filter() or .find() to pick the trailing match from a narrowed set, and use .first() for the opposite end.

💡 Best Practices

✅ Do

  • Use .last() when you always want the final element
  • Chain after .filter() to get the trailing matching item
  • Pair with .first() when styling both ends of a list
  • Call .end() after .last() when continuing on the full set
  • Prefer .last() over .eq(-1) for readability

❌ Don’t

  • Pass arguments to .last() — use .eq(n) instead
  • Confuse :last selector timing with .last() mid-chain
  • Assume .last() searches the DOM — it only reads the current set
  • Use .last() when you need the first item — use .first()
  • Forget that an empty collection stays empty after .last()

Key Takeaways

Knowledge Unlocked

Five things to remember about .last()

Final element — one item.

5
Core concepts
-1 02

.eq(-1)

Same

Equiv
03

Empty

Safe chain

Edge
1️⃣ 04

.first()

Opposite

Compare
🔗 05

Chain

After filter

Pattern

❓ Frequently Asked Questions

.last() reduces the current jQuery collection to the final element in the set. It returns a new jQuery object containing that single element, or an empty set if the collection was already empty.
No. .last() accepts no parameters. It always selects the last element in the current jQuery object. For other positions, use .eq(n) instead.
They produce the same result — both return the last element as a jQuery object. .last() is a readable shorthand when you always want the final item; .eq(-1) is explicit about negative index counting from the end.
.last() picks the final element in the set — equivalent to .eq(-1). .first() picks index 0. They are complementary shortcuts added together in jQuery 1.4 for the two ends of a collection.
.last() on an empty jQuery object returns another empty jQuery object with length 0. No error is thrown — chained methods simply do nothing.
:last is a jQuery selector pseudo-class used at query time — $('li:last'). .last() is a method on an existing collection — $('li').last(). Prefer .last() when you already have a jQuery object from .filter(), .find(), or another chain step.
Did you know?

jQuery’s .last() and .first() were added together in version 1.4 as readable shortcuts for .eq(-1) and .eq(0). They push onto the internal selection stack like other filtering methods, so you can call .end() afterward to return to the full matched set.

Back to jQuery Traversing

Explore more DOM traversal methods as the collection grows.

Traversing hub →

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.

6 people found this page helpful