Ramda.js R.swap() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.swap(indexA, indexB, list) returns a new list with the items at two positions exchanged (official Ramda docs). It also works on strings (two characters) and objects (two property values). The original is never changed, and negative indexes count from the end.

Added in v0.29.0: R.swap is stable — not deprecated, experimental, or non-standard — but it is undefined in Ramda 0.28 and earlier. JavaScript has no built-in swap; the destructuring trick [a[i], a[j]] = [a[j], a[i]] mutates the array.

Syntax

Call R.swap. Start with this line.

Ramda
R.swap(i, j, list) or R.swap(i, j)(list)
CallWhat it does
R.swap(i, j, list) or R.swap(i, j)(list)Primary signature.
R.swap(...)Call the list helper.

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

Library declarations
Ramda
R.swap(i, j, list) or R.swap(i, j)(list)

Parameters

ParameterDescription
indexARequired. Number or String. First position (a key name for objects)
indexBRequired. Number or String. Second position (a key name for objects)
listRequired. Array, String, or Object. The value to copy and reorder

Return Value

Return ValueDescription
*A new array, string, or object of the same kind

Ramda Examples

Example 1 — Lists, Objects, and Strings (Official Samples)

The same function swaps list items, object values, and characters.

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

console.log(R.swap(0, 2, letters));            // ['c', 'b', 'a', 'd', 'e', 'f']
console.log(R.swap(-1, 0, letters));           // ['f', 'b', 'c', 'd', 'e', 'a']
console.log(R.swap('a', 'b', { a: 1, b: 2 })); // { a: 2, b: 1 }
console.log(R.swap(0, 2, 'foo'));              // 'oof'
Try It Yourself

How It Works

  1. Two indexes. 'a' at 0 and 'c' at 2 trade places; the rest stay put.
  2. Negative index. -1 means the last item, so 'f' and 'a' swap.
  3. Object. For objects the “indexes” are key names, so the values of a and b swap.
  4. String. Characters 0 and 2 of 'foo' swap and are joined back into 'oof'.

Example 2 — Move a Task Up One Place

A “move up” button swaps an item with the one above it.

Ramda
const tasks = ['Wash', 'Dry', 'Fold', 'Iron'];
const moveUp = (i) => R.swap(i, i - 1);

console.log(moveUp(2)(tasks)); // ['Wash', 'Fold', 'Dry', 'Iron']
console.log(tasks);            // ['Wash', 'Dry', 'Fold', 'Iron']
Try It Yourself

How It Works

  1. Build the helper. moveUp(i) returns R.swap(i, i - 1), a function still waiting for the list.
  2. Swap neighbours. moveUp(2) exchanges 'Fold' (index 2) with 'Dry' (index 1).
  3. No mutation. Printing tasks again shows the original order — safe for React or Redux state.
  4. Watch the top. moveUp(0) would swap with index -1, the last item. Guard with i > 0.

Example 3 — Swap the First and Last Items

Curry the two indexes once, then reuse the function on any list or string.

Ramda
const swapEnds = R.swap(0, -1);

console.log(swapEnds([1, 2, 3, 4])); // [4, 2, 3, 1]
console.log(swapEnds('hello'));      // 'oellh'
Try It Yourself

How It Works

  1. Curry. Curried R.swap(0, -1) remembers the indexes and waits for the value.
  2. Any length. -1 always means the last item, so one helper fits every list.
  3. Array. 1 and 4 trade places; 2 and 3 stay.
  4. String. 'h' and 'o' swap, and a string comes back.

Example 4 — Swap Two Object Properties

Fix data where two fields were filled the wrong way round.

Ramda
const person = { first: 'Lovelace', last: 'Ada' };

console.log(R.swap('first', 'last', person)); // { first: 'Ada', last: 'Lovelace' }
console.log(R.swap('first', 'age', person));  // { first: 'Lovelace', last: 'Ada' }
Try It Yourself

How It Works

  1. Key names. For objects, pass the two property names instead of numbers.
  2. Values swap. The values of first and last are exchanged in a cloned object.
  3. Missing key. person has no age, so the second call returns an unchanged copy.
  4. No error. Ramda does not warn about the missing key — check the result if it matters.

Example 5 — R.swap vs R.move

Both reorder a list, but only swap leaves the items in between alone.

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

console.log(R.swap(0, 2, letters)); // ['c', 'b', 'a', 'd']
console.log(R.move(0, 2, letters)); // ['b', 'c', 'a', 'd']
Try It Yourself

How It Works

  1. swap. 'a' and 'c' trade seats; 'b' stays at index 1.
  2. move. R.move takes 'a' out and inserts it at index 2, so 'b' and 'c' shift left.
  3. Pick by intent. Exchange two items → swap. Drag one item to a new spot → move.
  4. Neighbours. For adjacent indexes like 1, 2, both give the same result.

Browser & Runtime Support

R.swap() is plain JavaScript inside Ramda 0.29.0+. It uses only slice and concat, so support depends only on loading a recent Ramda.

✓ Ramda 0.29.0+ · Any JS runtime

Ramda.js R.swap()

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 0.29+ 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.swap() Universal

Bottom line: Safe in any project that loads Ramda 0.29.0 or later — on older versions R.swap is undefined. 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).

Frequently Asked Questions

A new list, string, or object with the two positions exchanged. R.swap(0, 2, ['a', 'b', 'c']) returns ['c', 'b', 'a']. The original value is never changed.
Yes. A negative index counts from the end, so -1 is the last item. R.swap(-1, 0, ['a', 'b', 'c']) returns ['c', 'b', 'a'].
Ramda returns an unchanged copy and does not throw. One exception: an index equal to the list length is not treated as out of range, so R.swap(0, 3, ['a', 'b', 'c']) returns [undefined, 'b', 'c', 'a']. Check that indexes are below list.length.
R.swap exchanges two items and leaves the others in place. R.move takes one item out and inserts it elsewhere, so the items in between shift. R.swap(0, 2, ['a', 'b', 'c', 'd']) is ['c', 'b', 'a', 'd'], while R.move(0, 2, …) is ['b', 'c', 'a', 'd'].
Yes. Pass the two key names: R.swap('a', 'b', { a: 1, b: 2 }) returns { a: 2, b: 1 }. Keys must be strings and both must exist, otherwise you get an unchanged copy.
No. R.swap was added in Ramda v0.29.0 and is stable. It is not deprecated, experimental, or non-standard, but it does not exist in older Ramda versions.

Next: R.tail()

Get every item of a list or string except the first.

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