Ramda.js R.times() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

R.times(fn, n) calls fn n times, passing the index 0, 1, …, n − 1, and collects every result in a new array (official Ramda docs). It is the quickest way to generate a list instead of transforming one.

Added in v0.2.3: R.times is stable — not deprecated, experimental, or non-standard. Warning: n must be a whole number of 0 or more; negative, NaN, or decimal values throw a RangeError.

Syntax

Call R.times. Start with this line.

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

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

Library declarations
Ramda
R.times(fn, n) or R.times(fn)(n)

Parameters

ParameterDescription
fnRequired. Gets the index (0 to n − 1) and returns one item
nOptional. How many items to make; omit it to get a reusable function

Return Value

Return ValueDescription
*A new array holding every result of fn, in order

Ramda Examples

Example 1 — Official Sample and Squares

List the indexes themselves — the sample from the Ramda docs — then square each one.

Ramda
console.log(R.times(R.identity, 5));    // [0, 1, 2, 3, 4]
console.log(R.times(i => i * i, 5));   // [0, 1, 4, 9, 16]
Try It Yourself

How It Works

  1. Indexes. R.times calls the function with 0, 1, 2, 3, 4 — five calls for n = 5.
  2. Identity. R.identity returns its input unchanged, so the result is the indexes.
  3. Squares. i => i * i turns each index into its square.
  4. Zero first. Both lists start at 0 because the first index is always 0.

Example 2 — Numbers 1 to 10 and Their Sum

Shift the index by one to count from 1, then add the list up.

Ramda
const oneToTen = R.times(R.inc, 10);

console.log(oneToTen);           // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(R.sum(oneToTen));    // 55
Try It Yourself

How It Works

  1. Shift. R.inc adds 1, so indexes 0–9 become 1–10.
  2. Ten calls. n = 10 gives exactly ten numbers.
  3. Sum. R.sum adds them: 1 + 2 + … + 10 = 55.
  4. Tip. R.range(1, 11) gives the same numbers; R.times wins when each item needs more work.

Example 3 — Create Starter Records

Make three blank to-do items, each with its own id.

Ramda
const makeTodo = i => ({ id: i + 1, done: false });

const todos = R.times(makeTodo, 3);

todos.forEach(todo => console.log(todo));
// { id: 1, done: false }
// { id: 2, done: false }
// { id: 3, done: false }
Try It Yourself

How It Works

  1. Factory. makeTodo builds one object from an index.
  2. Ids. i + 1 turns indexes 0, 1, 2 into ids 1, 2, 3.
  3. Fresh objects. Each call creates a brand-new object, so changing one to-do never changes another.
  4. Output. The lab prints each object as JSON on its own line.

Example 4 — Build a Number Grid

Nest R.times to make 2 rows of 3 columns.

Ramda
const makeRow = row => R.times(col => row * 3 + col, 3);

const grid = R.times(makeRow, 2);

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

How It Works

  1. Outer. R.times(makeRow, 2) calls makeRow with row 0 and row 1.
  2. Inner. Each row runs its own R.times for columns 0, 1, 2.
  3. Cell value. row * 3 + col numbers the cells left to right: row 0 gives 0–2, row 1 gives 3–5.
  4. Separate rows. Every row is a new array, so editing one row leaves the other alone.

Example 5 — R.times vs R.repeat

Both make a list of empty arrays — but only one gives each slot its own array.

Ramda
const shared = R.repeat([], 2);
const fresh = R.times(() => [], 2);

shared[0].push('a');
fresh[0].push('a');

console.log(shared);   // [['a'], ['a']]
console.log(fresh);    // [['a'], []]
Try It Yourself

How It Works

  1. repeat. R.repeat([], 2) puts the same array in both slots.
  2. times. R.times(() => [], 2) calls the function twice, creating two different arrays.
  3. Push. Adding 'a' to slot 0 shows up in both slots of shared, but only in slot 0 of fresh.
  4. Rule. Use R.times whenever each item should be its own object or array.

Browser & Runtime Support

R.times() has been in Ramda since v0.2.3. It is a plain loop over an array, so it works wherever Ramda loads.

✓ Ramda 0.2.3+ · Any JS runtime

Ramda.js R.times()

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% Any Ramda version
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.times() Universal

Bottom line: Safe in every Ramda version and every modern or legacy runtime. Unlike native Array.from, it needs no ES2015 support. Load ramda.min.js from a CDN, or npm install ramda for Node, Deno, and Bun. These labs load [email protected].

Frequently Asked Questions

R.times(fn, n) calls fn n times, passing the index 0, 1, ..., n - 1, and returns the results as a new array. The official sample R.times(R.identity, 5) returns [0, 1, 2, 3, 4].
At 0. The function receives 0 on the first call and n - 1 on the last. For numbers that start at 1, use R.times(R.inc, n) or i => i + 1.
R.repeat(value, n) puts the same value in every slot. R.times(fn, n) calls a function for each slot, so each item can be different, and a function like () => [] creates a fresh object every time.
R.range(from, to) only builds a list of numbers. R.times builds any kind of value from the index. R.times(R.identity, 5) and R.range(0, 5) both return [0, 1, 2, 3, 4].
n = 0 returns []. A negative number, NaN, or undefined throws RangeError: n must be a non-negative number. A decimal such as 2.7 throws RangeError: Invalid array length, so round n first.
No. R.times has been part of Ramda since v0.2.3 and is stable. It is not deprecated, experimental, or non-standard.

Next: R.transduce()

Combine map, filter, and reduce into one efficient pass.

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