jQuery .slice() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Index range

What You’ll Learn

The .slice() traversing method reduces a jQuery collection to a subset by index range — with an optional end index that is exclusive, and negative indexes counting from the end. This tutorial covers the official list-item demos, random div slice button, pagination pattern, and compares .slice() with .eq(), .first(), .last(), and .filter().

01

Syntax

.slice(start [, end])

02

Zero-based

Index 0 = first

03

End exclusive

Like array slice

04

Negative

From end

05

vs .eq()

Range vs one

06

Since 1.1.4

Core API

Introduction

A jQuery selector like $("li") often matches multiple elements. Sometimes you need a contiguous block — items 3 through 5, the last two entries, or page 2 of a list. The .slice() method picks a range of elements from the current collection by their positions.

Available since jQuery 1.1.4, .slice(start [, end]) returns a new jQuery object containing only the elements in the specified index range. Indexes are zero-based and refer to the position within the jQuery object — not the element’s rank in the entire DOM document.

Understanding the .slice() Method

Given a jQuery object with zero or more matched elements, .slice(start [, end]) constructs a new jQuery object from elements at indexes start up to but not including end. If end is omitted, every element from start through the last index is included.

The method mirrors Array.prototype.slice: positive indexes count from the start; negative indexes count from the end. The range is always relative to the current jQuery collection, not the full DOM tree.

💡
Beginner Tip

The end index is exclusive. .slice(2, 4) selects indexes 2 and 3 (items 3 and 4) — not index 4.

📝 Syntax

General form of .slice:

jQuery
.slice( start [, end ] )

Parameters

  • start (required) — an integer indicating the 0-based position at which selection begins. A negative integer counts backwards from the end of the set.
  • end (optional) — an integer indicating the 0-based position at which selection stops. The element at this index is not included. Negative values count from the end. If omitted, the range continues to the last element.

Return value

  • A new jQuery object containing the subset of elements in the specified range — possibly empty if the range falls outside the set.

Official jQuery API list examples

jQuery
$( "li" ).slice( 2 ).css( "background-color", "red" );

// Items 3, 4, 5 → red (from index 2 to end)



$( "li" ).slice( 2, 4 ).css( "background-color", "red" );

// Items 3, 4 only (indexes 2–3; end 4 is exclusive)



$( "li" ).slice( -2, -1 ).css( "background-color", "red" );

// Item 4 only (between two from end and one from end)

⚡ Quick Reference

GoalCode
From index 2 to end$("li").slice(2)
Indexes 2 and 3 only$("li").slice(2, 4)
First element$("li").slice(0, 1) or .first()
Last element$("li").slice(-1) or .last()
Second and third (indexes 1–2)$("li").slice(1) on a 3-item set, or .slice(1, 3)
Page 2 (items 4–6, page size 3)$("li").slice(3, 6)

📋 .slice() vs .eq() vs .first() vs .last() vs .filter()

Five ways to narrow a collection — by index range, single index, endpoints, or criteria.

.slice(s, e)
index range

Contiguous subset by position; end exclusive

.eq(n)
one element

Single element at index n

.first()
slice(0,1)

Shorthand for the opening element

.last()
slice(-1)

Shorthand for the final element

.filter()
by rule

Selector or callback — not index-based

Examples Gallery

Examples 1–4 follow the official jQuery API documentation. Example 5 shows a pagination pattern. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery demos for index-range selection.

Example 1 — Official Demo: slice(2) From Index to End

Apply a red background to list items starting at index 2 — items 3, 4, and 5 in a five-item list.

jQuery
$( "li" ).slice( 2 ).css( "background-color", "red" );
Try It Yourself

How It Works

Omitting end includes every element from start through the last index. Index 2 is the third item in human counting.

Example 2 — Official Demo: slice(2, 4) Bounded Range

Select only indexes 2 and 3 — list items 3 and 4. The end index 4 is not included.

jQuery
$( "li" ).slice( 2, 4 ).css( "background-color", "red" );
Try It Yourself

How It Works

The range is half-open: inclusive at start, exclusive at end. Same rule as [].slice(2, 4) on a native array.

Example 3 — Official Demo: Negative Indices slice(-2, -1)

Select list item 4 only — the range between two from the end and one from the end.

jQuery
$( "li" ).slice( -2, -1 ).css( "background-color", "red" );
Try It Yourself

How It Works

Negative start and end resolve from the end of the set. On five items, index -2 is item 4 and -1 is item 5 — but end is exclusive, so only item 4 is selected.

📈 Practical Patterns

Random slice demo and pagination with .slice().

Example 4 — Official Demo: Random Slice on div Elements

Click a button to highlight a random contiguous range of divs — official jQuery slice demo pattern.

jQuery
function colorEm() {

  var $div = $( "div.slice-demo" );

  var start = Math.floor( Math.random() * $div.length );

  var end = Math.floor( Math.random() * ( $div.length - start ) ) +

    start + 1;

  if ( end === $div.length ) {

    end = undefined;

  }

  $div.css( "background", "" );

  if ( end ) {

    $div.slice( start, end ).css( "background", "yellow" );

  } else {

    $div.slice( start ).css( "background", "yellow" );

  }

  $( "span.slice-label" ).text(

    "$( 'div' ).slice( " + start +

    ( end ? ", " + end : "" ) +

    " ).css( 'background', 'yellow' );"

  );

}

$( "button.slice-btn" ).on( "click", colorEm );
Try It Yourself

How It Works

Random start and end values demonstrate that .slice() works on any jQuery collection size. Reset styles first, then apply yellow only to the sliced subset.

Example 5 — Pagination: Show Page 2 with slice(3, 6)

Display items 4, 5, and 6 from a nine-item list — page 2 when each page shows three items.

jQuery
var pageSize = 3;

var pageNum = 2; // human page number

var start = ( pageNum - 1 ) * pageSize; // → 3

var end = start + pageSize;             // → 6



$( "li" ).hide();

$( "li" ).slice( start, end ).show();

// Page 2 → indexes 3, 4, 5 (items 4, 5, 6)
Try It Yourself

How It Works

Pagination maps a human page number to a zero-based index range. .slice(3, 6) selects exactly three elements — the same count as pageSize because end is exclusive.

🚀 Common Use Cases

  • Pagination — show one page of results with .slice(start, end).
  • Load more — reveal the next batch with .slice(0, visibleCount) vs .slice(visibleCount, visibleCount + batch).
  • Trim collections — skip the first N items with .slice(n).
  • Last N items — combine negative start with no end: .slice(-3).
  • Carousel windows — display a sliding window of slides by index range.
  • After filtering — take a subset from a narrowed .filter() collection without re-querying the DOM.

🧠 How .slice() Builds a Subset

1

Start with collection

jQuery object holds N matched elements in order.

Input
2

Resolve indexes

Positive from start; negative from end. Clamp to valid bounds.

Index
3

Apply range

Include start through end − 1. Omit end → through last index.

Exclusive
4

Return new subset

Chain .css(), .hide(), or .addClass() on the result.

📝 Notes

  • Available since jQuery 1.1.4.
  • Indexes are zero-based and relative to the current jQuery collection.
  • The end index is exclusive — the element at end is not included.
  • Patterned after Array.prototype.slice; negative indexes count from the end.
  • .first() equals .slice(0, 1); .last() equals .slice(-1).
  • .eq(n) picks one element; .slice() picks a range — use the right tool for the job.
  • Does not modify the DOM — only narrows which elements the jQuery object refers to.

Browser Support

.slice() has been part of jQuery since 1.1.4+. It is pure collection indexing with no browser-specific behavior.

jQuery 1.1+

jQuery .slice()

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: Array.prototype.slice on a converted element array.

100% With jQuery loaded
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 in any jQuery project. Use .slice() for index-range subsets; use .eq() when you need exactly one element.

Conclusion

The jQuery .slice() method reduces the current collection to a subset defined by a zero-based index range — with an optional exclusive end index and negative offsets from the end. It is the standard way to work with contiguous blocks of matched elements while keeping jQuery chaining available.

Remember the official list lessons: .slice(2) selects from the third item onward; .slice(2, 4) selects items 3 and 4 only; .slice(-2, -1) picks a single element near the end. For pagination, map page number to start and end = start + pageSize.

💡 Best Practices

✅ Do

  • Use .slice(start, end) when you need a contiguous range
  • Remember end is exclusive — add 1 to the last index you want
  • Use negative indexes for “last N” patterns: .slice(-3)
  • Compute pagination with (page − 1) × pageSize for start
  • Apply .slice() after .filter() to subset results

❌ Don’t

  • Confuse human counting (1-based) with jQuery indexes (0-based)
  • Treat end as inclusive — .slice(0, 2) is two items, not three
  • Use .slice() when you need one element — prefer .eq(n)
  • Use .slice() for attribute-based selection — use .filter()
  • Assume indexes match global DOM position — they are collection-relative

Key Takeaways

Knowledge Unlocked

Five things to remember about .slice()

Index range subset.

5
Core concepts
0 02

Zero-based

Start at 0

Index
03

End exclusive

Like arrays

Bounds
−1 04

Negative

From end

Offset
.eq 05

vs eq

One vs many

Compare

❓ Frequently Asked Questions

.slice(start [, end]) reduces the current jQuery collection to a subset of elements 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 elements at indexes 2 and 3 only — not index 4. If end is omitted, all elements from start through the last index are included.
Negative start or end counts from the end of the set. On a five-item list, .slice(-2, -1) selects the element at 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, pagination, or trailing subsets.
jQuery .slice() is patterned after the native array method: zero-based indexes, end exclusive, and negative offsets from the end. The difference is 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.
Did you know?

The jQuery API explicitly notes that .slice() is patterned after JavaScript’s Array.prototype.slice, including support for negative start and end parameters. The official paragraph demos also show $("p").slice(0, 1), .slice(0, 2), .slice(1, 2), .slice(1), and .slice(-1) — the same half-open range rules apply to every call.

Back to jQuery Traversing

Explore more DOM traversal methods as the collection grows.

Traversing hub →

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.

6 people found this page helpful