jQuery .first() Method

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

What Is .first()?

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

Prefer over :first

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

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

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

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

How to Use .first()

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

  1. Select the elements you care about (li, .card, and so on).
  2. Call .first() — 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
.first()YesKeeps index 0 only
ArgumentsNoneAlways the first match

Pseudocode

Pseudocode
set = current jQuery collection

if set.length == 0:
    return empty jQuery

return jQuery(set[0])   // new one-element set

Cheat Sheet

Keep these facts handy while taking the first match.

TopicDetail
Syntax.first()
Index0 (document / set order)
vs .eq(0)Same result
vs .last()First vs last in the set
vs :firstPrefer method (selector deprecated 3.4)
Empty setReturns empty jQuery (no error)
SincejQuery 1.4

Live Preview

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

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

Result
$("li").first()
matched: 1
text: This is some text in a list.

/* index 0 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").first()

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

jQuery
var $first = $("ul li").first();
console.log($first.length);
console.log($first.text().trim());
Try It Yourself

How It Works

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

Example 2 — Same as .eq(0)

Both methods return the first element as a jQuery object.

jQuery
var $a = $("li").first();
var $b = $("li").eq(0);

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. .first(). Shorthand for the start of the set.
  2. .eq(0). Same index, written explicitly.
  3. Same DOM node. $a[0] === $b[0].
  4. See also. .eq() for any index.

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

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 .last() for the other end.

Example 4 — Chain After .find()

Build a descendant set, then take its first match.

jQuery
var $hit = $("#menu").find("li").first();
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 first. .first() keeps “Home”.
  4. Common pattern. Narrow, then pick the lead item.

Example 5 — Empty Set Is Safe

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

jQuery
var $hit = $(".missing").first();
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. .first() 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 .first() in real projects.

order

Document / set order

“First” means index 0 in the current jQuery set — usually document order, not visual left-to-right after CSS.

deprecated

Prefer over :first

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

empty

Empty set is silent

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

scope

Scope the set first

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

Browser Support

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

✓ jQuery 1.4+ · Core API

jQuery .first()

Works in all browsers with jQuery. Prefer .first() over the deprecated :first 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
.first() Core method

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

Performance Notes

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

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

Key Takeaways

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

One line: .first() means “keep index 0 of this set” — scope the set, then take the lead item.

Frequently Asked Questions

.first() reduces the current jQuery set to the first element (index 0) 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 last element, use .last() or .eq(-1).
They return the same element. .first() is a clear shorthand for index zero; .eq(0) makes the index explicit and pairs with .eq(-1) for the last item.
.first() picks index 0. .last() picks the final element in the set. They are complementary ends of the collection.
.first() returns an empty jQuery object (length 0). No error is thrown — further chain methods simply do nothing.
Prefer .first(). The :first selector is a jQuery extension deprecated in 3.4. Select with CSS first, then call .first().

Next: .last() Method

Reduce the matched set to the final element.

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