jQuery .slice() Method

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

What Is .slice()?

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

Remember
$("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) →            ✓      ✓
NeedUse
From index to end$set.slice(start)
Bounded range$set.slice(start, end)
One element by index.eq(n)
First / last only.first() / .last()
Odd / even indexes.odd() / .even()

How to Use .slice()

Select a set, pass a start index, optionally an exclusive end.

  1. Select the collection (e.g. $("li")).
  2. Call .slice(start) to keep from that index to the end.
  3. Or .slice(start, end) — end is not included.
  4. Use negatives to count from the end: .slice(-2) = last two.
  5. Chain styling or other methods on the subset.
PieceRequired?Role
Matched setYesCollection to reduce
startYes0-based start (negative = from end)
endNoExclusive stop; omit → through last
Return value—New jQuery set (0 or more)

Pseudocode

Pseudocode
call $(set).slice(start, end?)

n = set.length
resolve start (and end) like Array.slice:
    if negative → n + value
    clamp to [0, n]

if end omitted:
    end = n

result = elements from index start up to (not including) end
return result as a new jQuery object

Cheat Sheet

Keep index methods side by side.

TopicDetail
Syntax.slice(start [, end])
SincejQuery 1.1.4
Index baseZero-based in the jQuery set
endExclusive
NegativeOffset from the end
vs .eq()Range vs one element
ModelArray.prototype.slice
DOM change?No — query only

Live Preview

From five list items, try common .slice() ranges and compare with .eq().

  • list item 1
  • list item 2
  • list item 3
  • list item 4
  • list item 5

Official ideas: slice(2) → items 3–5; slice(2, 4) → items 3–4; slice(-2, -1) → item 4.

Result
Ready — choose a call and click Run.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

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.
  4. Mark. Items 3–5 get the suffix.

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.
  4. Remember. Same exclusive-end rule as arrays.

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.
  4. Same idea. Matches native array negative slicing.

Example 4 — .slice() vs .eq()

A range of one vs a single-index pick.

jQuery
$("#s-demo li").slice(1, 2).append(" [slice]");
$("#e-demo li").eq(1).append(" [eq]");
Try It Yourself

How It Works

  1. slice. (1, 2) is a half-open range with one element.
  2. eq. Picks index 1 directly.
  3. Same node. Both can target the second item.
  4. Pick. Prefer .eq() for one; .slice() for many.

Example 5 — Last Items & Simple Pages

Take the last item, or a page-sized window from the set.

jQuery
$("li").slice(-1).append(" [last]");

// Page 2 of size 2 (indexes 2–3):
// $("li").slice(2, 4);
Try It Yourself

How It Works

  1. -1. Start at the last index; no end → only the last item.
  2. Pages. .slice(offset, offset + size) is a simple page window.
  3. Clamp. Out-of-range pages return fewer or zero items.
  4. Chain. Style or move only that subset.

Edge Cases & Pitfalls

Why a slice can look one item short.

end

end is exclusive

.slice(0, 3) is three elements (0–2), not four. Same rule as arrays.

set

Indexes are in the jQuery set

Not DOM sibling order for unrelated nodes — only the current matched collection.

empty

Out of range → empty set

.slice(10) on five items returns length 0. No exception is thrown.

one

Need exactly one index?

Prefer .eq(n) over .slice(n, n + 1) for clarity.

Browser Support

.slice() has been part of jQuery since 1.1.4. It works in every browser that runs your loaded jQuery version — same indexing rules as Array.prototype.slice.

✓ Stable · jQuery 1.1.4+

jQuery .slice()

Supported across jQuery 1.x (1.1.4+), 2.x, and 3.x. Prefer a current jQuery 3.x build. Zero-based, end-exclusive, negative offsets from the end.

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

Bottom line: Safe for ranges, trailing subsets, and simple pagination windows. Remember: end is exclusive.

Performance Notes

ApproachCostBest for
.eq(n)CheapestOne element
.slice(a, b)CheapContiguous index range
.filter(fn)Per-elementNon-contiguous rules

Prefer a tight starting selector, then .slice(). Slicing only copies references into a new jQuery object — it does not walk the DOM tree.

Key Takeaways

  • Role: keep a contiguous index range from the set.
  • End: exclusive — .slice(2, 4) → indexes 2 and 3.
  • Negative: count from the end, like arrays.
  • One item: prefer .eq().

One line: .slice(start, end) keeps a half-open index range — just like Array.prototype.slice.

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

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

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

10 people found this page helpful