jQuery .first() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

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

Syntax

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

jQuery
$("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)
CallWhat it does
.first()Learn the jQuery .first() method: reduce a matched set to the first element, official list highlight demo, vs .eq(0) and .last(), 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 .first() — 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").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.

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

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.

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 — Final Match in a Set

Reduce the matched set to the final element.

.last() Method — Final Match in a Set 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