Ramda.js R.slice() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.slice(fromIndex, toIndex, list) returns a copy of part of a list or string: from fromIndex (included) up to toIndex (not included) (official Ramda docs). The original list is never changed. Because the list comes last and the function is curried, R.slice(1, 3) is a ready-made helper for pipelines.

Stable since v0.1.4: R.slice is not deprecated, experimental, or non-standard. It follows the same index rules as native Array.prototype.slice().

Syntax

Call R.slice. Start with this line.

Ramda
R.slice(from, to, list) or R.slice(from, to)(list)
CallWhat it does
R.slice(from, to, list) or R.slice(from, to)(list)Primary signature.
R.slice(...)Call the list helper.

Prefer import { slice } from "ramda" when you want a named import.

Library declarations
Ramda
R.slice(from, to, list) or R.slice(from, to)(list)

Parameters

ParameterDescription
fromIndexRequired. Number. Start position, inclusive. Negative counts from the end.
toIndexRequired. Number. Stop position, exclusive. Infinity means the end; negative counts from the end.
listOptional. Array, String, or object with a slice method. The source to copy from; omit it to get a reusable function

Return Value

Return ValueDescription
*A new array, or a new string for string input

Ramda Examples

Example 1 — Start and End Index (Official Sample)

Keep a middle window, then keep everything from index 1 with Infinity.

Ramda
const letters = ['a', 'b', 'c', 'd'];

console.log(R.slice(1, 3, letters));        // ['b', 'c']
console.log(R.slice(1, Infinity, letters)); // ['b', 'c', 'd']
console.log(letters);                       // ['a', 'b', 'c', 'd']
Try It Yourself

How It Works

  1. Number the items. a is index 0, b is 1, c is 2, d is 3.
  2. Start is kept. R.slice(1, 3) begins at index 1 ('b').
  3. End is not kept. It stops before index 3, so the result is ['b', 'c'] — two items, because 3 − 1 = 2.
  4. To the end. Infinity as the end means “keep going until the list runs out”.
  5. No mutation. letters still has all four items.

Example 2 — Negative Indexes Count from the End

Drop the last item, take a window near the end, and grab the last two items.

Ramda
const letters = ['a', 'b', 'c', 'd'];

console.log(R.slice(0, -1, letters));        // ['a', 'b', 'c']
console.log(R.slice(-3, -1, letters));       // ['b', 'c']
console.log(R.slice(-2, Infinity, letters)); // ['c', 'd']
Try It Yourself

How It Works

  1. Add the length. The list has 4 items, so -1 becomes index 3 and -3 becomes index 1.
  2. Drop the last. R.slice(0, -1) is R.slice(0, 3) → ['a', 'b', 'c'].
  3. Window from the end. R.slice(-3, -1) is R.slice(1, 3) → ['b', 'c'].
  4. Last two. R.slice(-2, Infinity) starts at index 2 and runs to the end → ['c', 'd']. R.takeLast(2) does the same.

Example 3 — Slice a String

The same rules work on text, and the result stays a string.

Ramda
const word = 'ramda';

console.log(R.slice(0, 3, word));          // 'ram'
console.log(R.slice(-2, Infinity, word));  // 'da'
console.log(typeof R.slice(0, 3, word));   // 'string'
Try It Yourself

How It Works

  1. Characters are items. In 'ramda', r is index 0 and a (last) is index 4.
  2. First three. R.slice(0, 3) keeps indexes 0, 1, 2 → 'ram'.
  3. Last two. -2 becomes index 3, so the result is 'da'.
  4. Type is kept. Strings have their own slice method, and Ramda calls it, so you get a string back, not an array.

Example 4 — A Reusable Pagination Helper

Build page windows once with the curried form, then apply them to any list.

Ramda
const items = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];

const page = (n, size) => R.slice((n - 1) * size, n * size);

console.log(page(1, 3)(items)); // ['A', 'B', 'C']
console.log(page(2, 3)(items)); // ['D', 'E', 'F']
console.log(page(3, 3)(items)); // ['G']
Try It Yourself

How It Works

  1. Curry. R.slice(from, to) has no list yet, so it returns a function that waits for one.
  2. Page math. Page 2 with size 3 gives R.slice(3, 6) → indexes 3, 4, 5 → ['D', 'E', 'F'].
  3. Short last page. Page 3 asks for R.slice(6, 9), but only index 6 exists, so the end is clamped → ['G'].
  4. No errors. A page past the end, like page(4, 3), simply returns [].

Example 5 — Top 3 Scores in a Pipeline

Sort a copy from high to low, then keep the first three — without touching the original.

Ramda
const scores = [42, 97, 65, 88, 71, 90];

const top3 = R.pipe(
  R.sort((a, b) => b - a), // highest first
  R.slice(0, 3)            // keep indexes 0, 1, 2
);

console.log(top3(scores)); // [97, 90, 88]
console.log(scores);       // [42, 97, 65, 88, 71, 90]
Try It Yourself

How It Works

  1. Sort a copy. R.sort returns a new array ordered high to low: [97, 90, 88, 71, 65, 42].
  2. Slice the front. R.slice(0, 3) keeps the first three → [97, 90, 88].
  3. Pipe them. R.pipe runs the steps left to right, so top3 is one reusable function.
  4. Original safe. Neither step mutates, so scores keeps its order. R.slice(0, 3) is the same as R.take(3).

Browser & Runtime Support

R.slice() is plain JavaScript inside Ramda 0.1.4+. It only calls the built-in slice method that arrays and strings have had since ES3, so support depends only on loading Ramda.

✓ Ramda 0.1.4+ · Any JS runtime

Ramda.js R.slice()

The UMD build (ramda.min.js) uses ES5 syntax, so it runs in every modern browser. Via npm it works in Node.js, Deno, and Bun with require or import.

100% With Ramda loaded
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer End of life · ES5 build may load, untested
Legacy only
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Node.js Supported · npm install ramda
Supported
Deno Supported · npm:ramda
Supported
Bun Supported · bun add ramda
Supported
R.slice() Universal

Bottom line: Safe in any project that loads Ramda. Use ramda.min.js from a CDN in the browser, or npm install ramda for Node, Deno, and Bun. The Try-it labs load [email protected] and use modern syntax (const, arrow functions).

Frequently Asked Questions

A new array (or a new string) holding the items from fromIndex up to, but not including, toIndex. The original list is never changed. For example, R.slice(1, 3, ['a', 'b', 'c', 'd']) returns ['b', 'c'].
No. The start index is inclusive and the end index is exclusive, so the result has toIndex - fromIndex items when both are in range. R.slice(1, 3, list) returns two items: index 1 and index 2.
A negative index counts back from the end, so -1 is the last position. R.slice(0, -1, list) drops the last item. Infinity as the end index means 'through the end of the list', so R.slice(2, Infinity, list) keeps everything from index 2.
They return the same values. Native slice is a method called on the data: list.slice(1, 3). R.slice takes the data last and is curried: R.slice(1, 3, list), or R.slice(1, 3) as a reusable function for R.pipe. R.slice also works on strings and any object with a slice method.
Use R.take(n) for the first n items and R.drop(n) to skip the first n items; they read more clearly. Use R.slice when you need a window with both a start and an end, like a page of results.
No. R.slice has been a stable Ramda List function since v0.1.4. It is not deprecated, experimental, or non-standard.

Next: R.sort()

Return a sorted copy of a list with your own comparator.

R.sort() 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.

6 people found this page helpful