Ramda.js R.repeat() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.repeat(value, n) builds a new array that holds value exactly n times. It is handy for default values, placeholders, and building strings from a repeated piece (official Ramda docs).

Stable since v0.1.1: R.repeat is not deprecated, experimental, or non-standard. It does not copy the value: repeated objects and arrays all share one reference.

Syntax

Call R.repeat. Start with this line.

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

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

Library declarations
Ramda
R.repeat(value, n) or R.repeat(value)(n)

Parameters

ParameterDescription
valueRequired. The item placed in every slot (not copied)
nOptional. How many slots; a whole number >= 0. Omit it to get a reusable function

Return Value

Return ValueDescription
*A new array of length n

Ramda Examples

Example 1 — Official: Repeat a Word

The first sample from the Ramda docs: five copies of 'hi'.

Ramda
console.log(R.repeat('hi', 5));
// ['hi', 'hi', 'hi', 'hi', 'hi']
Try It Yourself

How It Works

  1. Value. 'hi' is the item to repeat.
  2. Count. 5 means the new array has five slots.
  3. Fill. Each slot gets 'hi'.
  4. Result. A brand-new array of length 5.

Example 2 — Official: Every Slot Is the Same Object

The second docs sample shows that objects are shared, not copied.

Ramda
const obj = {};
const repeatedObjs = R.repeat(obj, 5);

console.log(repeatedObjs[0] === repeatedObjs[1]);  // true
Try It Yourself

How It Works

  1. One object. obj is created once.
  2. Five slots. R.repeat puts that same object into all five slots.
  3. Same reference. === compares identity, and slot 0 and slot 1 point to one object, so it prints true.
  4. Why it matters. Changing repeatedObjs[0] also changes every other slot (see the next example).

Example 3 — Shared Arrays vs Fresh Arrays

Pushing into one shared row changes them all. R.times builds a fresh array per slot.

Ramda
const shared = R.repeat([], 3);
shared[0].push('x');
console.log(shared);  // [['x'], ['x'], ['x']]

const fresh = R.times(() => [], 3);
fresh[0].push('x');
console.log(fresh);   // [['x'], [], []]
Try It Yourself

How It Works

  1. Shared. R.repeat([], 3) evaluates [] once, so all three slots hold one array.
  2. Surprise. Pushing 'x' into slot 0 shows up in every slot.
  3. Fresh. R.times calls () => [] three times, creating three separate arrays.
  4. Rule. Use R.repeat for numbers and strings; use R.times when each slot needs its own object or array.

Example 4 — Build Strings with R.join

Repeat a piece, then join the array into one string.

Ramda
console.log(R.join('', R.repeat('-', 10)));
// ----------

const stars = (n) => R.join('', R.repeat('*', n));
console.log(R.map(stars, [1, 2, 3]));
// ['*', '**', '***']
Try It Yourself

How It Works

  1. Repeat. R.repeat('-', 10) makes an array of ten dashes.
  2. Join. R.join('') glues them together with nothing in between: a divider line.
  3. Reuse. stars(n) does the same with '*' for any length.
  4. Map. R.map calls stars for 1, 2, and 3, giving a tiny triangle.

Example 5 — Default Values and a Reusable Builder

Pair keys with a repeated default, and curry R.repeat into a helper.

Ramda
const seats = ['A1', 'A2', 'A3'];
console.log(R.zipObj(seats, R.repeat('free', seats.length)));
// { A1: 'free', A2: 'free', A3: 'free' }

const zeros = R.repeat(0);
console.log(zeros(4));  // [0, 0, 0, 0]
Try It Yourself

How It Works

  1. Match the length. R.repeat('free', seats.length) gives one 'free' per seat.
  2. Pair up. R.zipObj uses the seats as keys and the repeated values as values.
  3. Curry. R.repeat(0) has no n yet, so it returns a function.
  4. Reuse. zeros(4) supplies n and returns four zeros.

Browser & Runtime Support

R.repeat() is plain JavaScript inside Ramda 0.1.1+. It uses no browser API, so support depends only on loading Ramda. Without Ramda, new Array(n).fill(value) (ES2015) builds the same array, with the same shared-reference behavior.

✓ Ramda 0.1.1+ · Any JS runtime

Ramda.js R.repeat()

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

Bottom line: Safe in any project with Ramda (check with npm ls ramda). Load ramda.min.js from a CDN in the browser, or npm install ramda for Node, Deno, and Bun. These labs load [email protected].

Frequently Asked Questions

R.repeat(value, n) returns a new array that contains value exactly n times. The official sample R.repeat('hi', 5) returns ['hi', 'hi', 'hi', 'hi', 'hi'].
No. Every slot holds the same reference. In the official sample, repeatedObjs[0] === repeatedObjs[1] is true, so changing one item changes them all. Use R.times(() => ({}), n) when each slot needs its own object.
R.repeat(value, n) puts the same value in every slot. R.times(fn, n) calls fn(index) for each slot, so it can build a different or fresh value each time. In fact, Ramda builds repeat as R.times(R.always(value), n).
R.repeat returns an array, so join it: R.join('', R.repeat('-', 10)) gives '----------'. In plain JavaScript, '-'.repeat(10) does the same thing.
Both throw a RangeError. A negative n gives 'n must be a non-negative number', and a decimal such as 2.5 gives 'Invalid array length'. An n of 0 returns an empty array.
No. R.repeat has been a stable Ramda list function since v0.1.1. It is not deprecated, experimental, or non-standard.

Next: R.reverse()

Return a list or string in reverse order.

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