Ramda.js R.splitAt() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.splitAt(index, list) cuts a list or string into two parts at an index and returns them as a pair: everything before the index, then everything from the index on (official Ramda docs). The original is never changed, and R.splitAt(n) is a reusable function for pipelines.

Stable since v0.19.0: R.splitAt is not deprecated, experimental, or non-standard. JavaScript has no native splitAt; the closest is two calls to Array.prototype.slice().

Syntax

Call R.splitAt. Start with this line.

Ramda
R.splitAt(n, list) or R.splitAt(n)(list)
CallWhat it does
R.splitAt(n, list) or R.splitAt(n)(list)Primary signature.
R.splitAt(...)Call the list helper.

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

Library declarations
Ramda
R.splitAt(n, list) or R.splitAt(n)(list)

Parameters

ParameterDescription
indexRequired. Number. Where part 2 starts. Negative counts from the end.
listOptional. Array or String. The value to cut; omit it to get a reusable function

Return Value

Return ValueDescription
*A pair: [before, from] — two arrays or two strings

Ramda Examples

Example 1 — Split a List (Official Sample)

Cut [1, 2, 3] after the first item.

Ramda
const nums = [1, 2, 3];

console.log(R.splitAt(1, nums)); // [[1], [2, 3]]
console.log(nums);               // [1, 2, 3]
Try It Yourself

How It Works

  1. Number the items. 1 is index 0, 2 is index 1, 3 is index 2.
  2. Cut before index 1. Everything before it, [1], becomes part 1.
  3. The rest is part 2. The item at index 1 and everything after: [2, 3].
  4. A new pair. Both parts are new arrays; nums is unchanged.

Example 2 — Strings and Negative Indexes (Official Samples)

Strings split into two strings, and a negative index counts from the end.

Ramda
console.log(R.splitAt(5, 'hello world')); // ['hello', ' world']
console.log(R.splitAt(-1, 'foobar'));      // ['fooba', 'r']
Try It Yourself

How It Works

  1. Characters are items. In 'hello world', index 5 is the space.
  2. Space goes right. The cut is before index 5, so the space starts part 2: ' world'.
  3. Negative index. 'foobar' has 6 characters, so -1 means index 5 — the final 'r'.
  4. Strings in, strings out. You get two strings, not arrays of characters.

Example 3 — Separate a Header from the Rows

Unpack the pair with destructuring — a common pattern for CSV lines.

Ramda
const lines = ['name,age', 'Ana,31', 'Ben,25', 'Cy,40'];

const [header, rows] = R.splitAt(1, lines);

console.log(header[0]);   // 'name,age'
console.log(rows);        // ['Ana,31', 'Ben,25', 'Cy,40']
console.log(rows.length); // 3
Try It Yourself

How It Works

  1. Split after one line. R.splitAt(1, lines) returns [['name,age'], ['Ana,31', 'Ben,25', 'Cy,40']].
  2. Destructure. const [header, rows] gives each part its own name in one line.
  3. Header is an array. Part 1 is still a list, so the header text is header[0]. Use R.head if you only need that one line.
  4. Rows are ready. rows holds the three data lines for further processing.

Example 4 — Split at the First Match

Find the index of the first even number, then cut there.

Ramda
const isEven = (n) => n % 2 === 0;
const values = [1, 3, 5, 8, 9];

const i = R.findIndex(isEven, values);

console.log(i);                           // 3
console.log(R.splitAt(i, values));        // [[1, 3, 5], [8, 9]]
console.log(R.splitWhen(isEven, values)); // [[1, 3, 5], [8, 9]]
Try It Yourself

How It Works

  1. Find the position. R.findIndex returns 3, the index of 8.
  2. Cut there. R.splitAt(3, values) puts the odd numbers before 8 in part 1 and [8, 9] in part 2.
  3. One-step version. R.splitWhen(isEven) finds and cuts in a single call.
  4. Watch for −1. If nothing matches, findIndex returns -1 and splitAt cuts off the last item. splitWhen safely returns [list, []].

Example 5 — Rotate a List in a Pipeline

Split, swap the two parts, then flatten them back into one list.

Ramda
const rotate = (n) => R.pipe(
  R.splitAt(n), // [[1, 2], [3, 4, 5]]
  R.reverse,    // [[3, 4, 5], [1, 2]]
  R.unnest      // [3, 4, 5, 1, 2]
);

console.log(rotate(2)([1, 2, 3, 4, 5]));  // [3, 4, 5, 1, 2]
console.log(rotate(-1)([1, 2, 3, 4, 5])); // [5, 1, 2, 3, 4]
Try It Yourself

How It Works

  1. Curried split. R.splitAt(2) waits for a list, so it fits straight into R.pipe.
  2. Swap the parts. R.reverse on the pair puts part 2 first.
  3. Join them. R.unnest removes one level of nesting → [3, 4, 5, 1, 2].
  4. Negative works too. rotate(-1) splits off the last item and moves it to the front.

Browser & Runtime Support

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

✓ Ramda 0.19.0+ · Any JS runtime

Ramda.js R.splitAt()

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.splitAt() Universal

Bottom line: Safe in any project that loads Ramda 0.19.0 or later. 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, destructuring).

Frequently Asked Questions

A two-item array: everything before the index, then everything from the index on. R.splitAt(1, [1, 2, 3]) returns [[1], [2, 3]]. For a string you get two strings: R.splitAt(5, 'hello world') returns ['hello', ' world'].
The second part. The cut happens just before the index, so the first part is slice(0, index) and the second is slice(index, length). In R.splitAt(1, [1, 2, 3]), the item at index 1 (the number 2) starts the second part.
Yes. A negative index counts from the end, so R.splitAt(-1, 'foobar') returns ['fooba', 'r']. Watch out: R.findIndex returns -1 when nothing matches, and splitting at -1 cuts off the last item.
Nothing breaks. The first part gets every item and the second part is empty: R.splitAt(9, ['a', 'b']) returns [['a', 'b'], []]. An index of 0 gives the opposite: [[], list].
R.splitAt(n, list) equals [R.take(n, list), R.drop(n, list)] in one call. R.splitEvery(n, list) cuts into many chunks of size n. R.splitWhen(pred, list) cuts before the first item that passes a test instead of at a fixed index.
No. R.splitAt has been a stable Ramda List function since v0.19.0. It is not deprecated, experimental, or non-standard.

Next: R.splitEvery()

Cut a list or string into equal-sized chunks.

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