jQuery .slice() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.slice(start [, end]) (since jQuery 1.1.4) reduces the matched set to a subset by zero-based index range. Indexes refer to position in the jQuery object — not the DOM tree. Patterned after JavaScript Array.prototype.slice.

Syntax

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

jQuery
$("li").slice(2);          // items 3–5 (from index 2 to end)
$("li").slice(2, 4);       // items 3–4 (end exclusive)
$("li").slice(-2, -1);     // item 4 only (official negative demo)
$("li").slice(-1);         // last item

// Indexes:  0      1      2      3      4
// Items:    1st    2nd    3rd    4th    5th
// slice(2) →              ✓      ✓      ✓
// slice(2,4) →            ✓      ✓
CallWhat it does
.slice()Learn jQuery .slice(): reduce a matched set by zero-based start/end indexes, negative offsets from the end, end-exclusive ranges like Array.slice, 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 collection (e.g. $("li")).
Step 2Call .slice(start) to keep from that index to the end.
Step 3Or .slice(start, end) — end is not included.

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: .slice(2)

Keep every item from index 2 to the end (jQuery API demo).

jQuery
$("li").slice(2).append(" [from 2]");
Try It Yourself

How It Works

  1. Select. Start with every li.
  2. Start. Index 2 is the third item.
  3. No end. The range continues to the last item.

Example 2 — Official: .slice(2, 4)

End is exclusive — keeps indexes 2 and 3 only.

jQuery
$("li").slice(2, 4).append(" [2..4)");
Try It Yourself

How It Works

  1. Start. Begin at index 2.
  2. End. Stop before index 4.
  3. Keep. Only indexes 2 and 3 remain.

Example 3 — Official: .slice(-2, -1)

Negative indexes count from the end of the set.

jQuery
$("li").slice(-2, -1).append(" [neg]");
Try It Yourself

How It Works

  1. -2. Two from the end → index 3 on a five-item list.
  2. -1. One from the end → exclusive stop at index 4.
  3. Range. Only index 3 (item 4) is kept.

Frequently Asked Questions

.slice(start [, end]) reduces the current jQuery collection to a subset within a zero-based index range. It returns a new jQuery object — the original set is unchanged.
Exclusive, like Array.prototype.slice. .slice(2, 4) includes indexes 2 and 3 only — not index 4. If end is omitted, the range continues to the end of the set.
Negative start or end counts from the end. On a five-item list, .slice(-2, -1) selects index 3 (list item 4) — from two from the end up to but not including one from the end.
.eq(n) returns exactly one element at index n. .slice(start, end) returns zero or more elements in a range. Use .eq() for a single pick; use .slice() for batches or trailing subsets.
It is patterned after the native array method: zero-based indexes, end exclusive, and negative offsets from the end. It operates on a jQuery object of DOM elements and returns another jQuery object.
jQuery clamps to valid bounds — no error is thrown. An empty or partial range returns fewer elements. For example, .slice(10) on a five-item set returns an empty jQuery object.

Next: .even() Method — Select Even Indexes

Keep even-indexed elements from the matched set (jQuery 3.5+).

.even() Method — Select Even Indexes 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.

10 people found this page helpful