Ramda.js R.intersperse() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.intersperse(separator, list) returns a new array with separator placed in every gap between two items. Nothing is added before the first item or after the last, and the original list is not changed (official Ramda docs).

Stable since v0.14.0: R.intersperse is not deprecated, experimental, or non-standard. Note: it always returns an array; use R.join when you want a string.

Syntax

Call R.intersperse. Start with this line.

Ramda
R.intersperse(separator, list)
CallWhat it does
R.intersperse(separator, list)Primary signature.
R.intersperse(...)Call the list helper.

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

Library declarations
Ramda
R.intersperse(separator, list)

Parameters

ParameterDescription
separatorRequired. Any. The value placed in each gap
listRequired. Array. The items to separate (not changed)

Return Value

Return ValueDescription
*The value returned by R.intersperse().

Ramda Examples

Example 1 — Official Sample

Put 'a' between four letters, as in the Ramda docs, then join them.

Ramda
const letters = R.intersperse('a', ['b', 'n', 'n', 's']);

console.log(JSON.stringify(letters)); // ["b","a","n","a","n","a","s"]
console.log(R.join('', letters));     // bananas
Try It Yourself

How It Works

  1. Argument order. Separator first, list last.
  2. Gaps. Four letters have three gaps, so 'a' is added three times.
  3. Still an array. The result has 7 items; nothing is added at either end.
  4. Join. R.join('') glues the letters into bananas.

Example 2 — Empty, One Item, and Two Items

A separator only appears where there is a gap between two items.

Ramda
console.log(JSON.stringify(R.intersperse('-', [])));         // []
console.log(JSON.stringify(R.intersperse('-', ['only'])));   // ["only"]
console.log(JSON.stringify(R.intersperse('-', ['a', 'b']))); // ["a","-","b"]
Try It Yourself

How It Works

  1. Empty. Zero items means zero gaps, so you get a new empty array.
  2. One item. Still no gap, so no separator: ["only"].
  3. Two items. One gap, one separator: 3 items in total.
  4. Rule. n items (n ≥ 1) always give 2n − 1 items.

Example 3 — Build a Breadcrumb

Keep each page name and each > as separate pieces, then turn them into text.

Ramda
const crumbs = ['Home', 'Ramda', 'List'];
const parts = R.intersperse('>', crumbs);

console.log(JSON.stringify(parts)); // ["Home",">","Ramda",">","List"]
console.log(R.join(' ', parts));   // Home > Ramda > List
Try It Yourself

How It Works

  1. Separate. '>' goes between each pair of page names.
  2. Pieces kept. Each name and each '>' is its own item, so a UI could show names as links and arrows as plain text.
  3. Text. R.join(' ', parts) puts a space between every piece.
  4. Unchanged. crumbs still has 3 items.

Example 4 — intersperse vs join

Same words and separator, two different kinds of result.

Ramda
const colors = ['red', 'green', 'blue'];

console.log(JSON.stringify(R.intersperse(', ', colors))); // ["red",", ","green",", ","blue"]
console.log(JSON.stringify(R.join(', ', colors)));        // "red, green, blue"
Try It Yourself

How It Works

  1. intersperse. Returns an array of 5 items; the separator is an item.
  2. join. R.join returns one string, shown in quotes by JSON.stringify.
  3. Same order. Both put ', ' only between items, never at the ends.
  4. Rule of thumb. Want text → join. Want a list to keep working with → intersperse.

Example 5 — A Reusable “Add Dividers” Step

Leave out the list to get a function that puts a divider object between menu items.

Ramda
const addDividers = R.intersperse({ divider: true });

const menu = addDividers([{ label: 'Open' }, { label: 'Save' }, { label: 'Quit' }]);
const toText = R.map(item => (item.divider ? '---' : item.label));

console.log(menu.length);               // 5
console.log(R.join('\n', toText(menu)));
// Open
// ---
// Save
// ---
// Quit
Try It Yourself

How It Works

  1. Curry. R.intersperse({ divider: true }) has its separator, so it waits for a list.
  2. Any value. The separator is an object; it fills both gaps, giving 5 items.
  3. Render. R.map turns dividers into '---' and items into their labels.
  4. Reuse. Call addDividers on any menu, or drop it into R.pipe.

Browser & Runtime Support

R.intersperse() has been in Ramda since v0.14.0. It only uses a plain loop and Array, which every JavaScript engine supports.

✓ Ramda 0.14.0+ · Any JS runtime

Ramda.js R.intersperse()

The UMD build (ramda.min.js) is compiled to ES5 syntax, so it runs in Chrome, Firefox, Safari, and Edge. Via npm it works in Node.js, Deno, and Bun with require or import.

100% With Ramda 0.14+ 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 No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
R.intersperse() Universal

Bottom line: Safe in any project with Ramda 0.14.0 or later. JavaScript has no native Array.prototype.intersperse; the closest one-liner uses flatMap, which needs ES2019: Chrome 69+, Firefox 62+, Safari 12+, Node 11+. These labs load [email protected].

Frequently Asked Questions

R.intersperse(separator, list) returns a new array with the separator placed between every pair of items. A list of n items (n ≥ 1) becomes 2n − 1 items. The original list is never changed.
R.intersperse returns an array and keeps each separator as its own item: R.intersperse(', ', ['a', 'b']) is ['a', ', ', 'b']. R.join returns one string: R.join(', ', ['a', 'b']) is 'a, b'.
No separator is added, because there is no gap between items. R.intersperse('-', []) is [] and R.intersperse('-', ['only']) is ['only'].
No. It creates a new array and copies the items and separators into it. If the separator is an object, the same object fills every gap, so changing one changes them all.
There is no Array.prototype.intersperse. list.flatMap((x, i) => (i ? [sep, x] : [x])) gives the same result and needs ES2019 (Chrome 69, Firefox 62, Safari 12, Node 11).
No. R.intersperse has been a stable Ramda List function since v0.14.0. It is not deprecated, experimental, or non-standard.

Next: R.into()

Reduce a list into a new collection with a transducer.

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