Home jQuery Traversing .first() jQuery .first() Method Definition
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().
$("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) Approach
How to Use .first() Build a set, then keep only the element at index zero.
Select the elements you care about (li, .card, and so on). Call .first() — 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 .first()Yes Keeps index 0 only Arguments None Always the first match
Pseudocode set = current jQuery collection
if set.length == 0:
return empty jQuery
return jQuery(set[0]) // new one-element set Reference
Cheat Sheet Keep these facts handy while taking the first match.
Topic Detail Syntax .first()Index 0 (document / set order)vs .eq(0) Same result vs .last() First vs last in the set vs :first Prefer method (selector deprecated 3.4) Empty set Returns empty jQuery (no error) Since jQuery 1.4
Try it
Live Preview Compare .first(), .eq(0), and .last() 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 first 2. vs .eq(0) 3. vs .last() 4. After .find() 5. Empty set Example 1 — Official Demo: $("ul li").first() Take the first list item in document order (jQuery API example).
var $first = $("ul li").first();
console.log($first.length);
console.log($first.text().trim()); 1
This is some text in a list. How It Works Match all items. $("ul li") builds the full set.Keep index 0. .first() drops every later item.One-element set. Length is 1.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.
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]); list item 1
list item 1
same node: true How It Works .first(). Shorthand for the start of the set..eq(0). Same index, written explicitly.Same DOM node. $a[0] === $b[0].See also. .eq() for any index.Example 3 — .first() vs .last() 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 .last() for the other end.Example 4 — Chain After .find() Build a descendant set, then take its first match.
var $hit = $("#menu").find("li").first();
console.log($hit.text().trim());
console.log("from find count:", $("#menu").find("li").length); How It Works Scope. $("#menu") is the root.Find items. .find("li") collects three links.Take first. .first() keeps “Home”.Common pattern. Narrow, then pick the lead item.Example 5 — Empty Set Is Safe Calling .first() on zero matches returns an empty jQuery object.
var $hit = $(".missing").first();
console.log("length:", $hit.length);
console.log("text:", JSON.stringify($hit.text())); How It Works No matches. $(".missing") is empty..first() 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 .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.
Compatibility
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.
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.
Analysis
Performance Notes Approach Cost Best for $("#id li").first()Scoped query + O(1) pick Typical UI work $("li").first()Full page query first Small 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().
Remember
Key Takeaways One line: .first() means “keep index 0 of this set” — scope the set, then take the lead item.
Frequently Asked Questions What does jQuery .first() do? .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.
Does .first() take arguments? No. It accepts no parameters. For other positions, use .eq(n). For the last element, use .last() or .eq(-1).
How is .first() different from .eq(0)? 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.
How is .first() different from .last()? .first() picks index 0. .last() picks the final element in the set. They are complementary ends of the collection.
What if the set is empty? .first() returns an empty jQuery object (length 0). No error is thrown — further chain methods simply do nothing.
Should I use .first() or :first? Prefer .first(). The :first selector is a jQuery extension deprecated in 3.4. Select with CSS first, then call .first().
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