Ramda.js R.forEach() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
List

Definition

A helper that calls a function once for every item in a list, from left to right, and then returns the same list it was given. Use it for side effects such as logging, updating the page, or sending data, not for building new values. It has been part of Ramda List since v0.1.1 (stable, not deprecated, experimental, or non-standard).

Why not the native method? Array.prototype.forEach returns undefined. R.forEach hands the list back, so it can sit in the middle of a R.pipe, and it is curried for reuse.

Syntax

Call R.forEach. Start with this line.

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

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

Library declarations
Ramda
R.forEach(fn, list)

Parameters

ParameterDescription
fnRequired. value → *. Called for each item, and its result is ignored
listRequired. An array, or any object with its own forEach method

Return Value

Return ValueDescription
*The original list (the same reference)

Ramda Examples

Example 1 — Official Sample

Log each number plus five, then look at what comes back.

Ramda
const printXPlusFive = x => console.log(x + 5);

const result = R.forEach(printXPlusFive, [1, 2, 3]);
// logs 6, 7, 8

console.log(result);
// → [1, 2, 3]
Try It Yourself

How It Works

  1. The action. printXPlusFive logs a value, which is a side effect, and returns nothing.
  2. Each item. R.forEach calls it with 1, 2, and 3 in order, so it logs 6, 7, and 8.
  3. Return value. R.forEach returns the list it was given: [1, 2, 3].
  4. Native difference. [1, 2, 3].forEach(...) would return undefined instead.

Example 2 — A Logging Step Inside a Pipeline

Because the list comes back, you can peek at it mid-pipeline without breaking the chain.

Ramda
const process = R.pipe(
  R.filter(n => n % 2 === 0),
  R.forEach(n => console.log("even:", n)),
  R.map(n => n * 10)
);

console.log(process([1, 2, 3, 4]));
// logs even: 2, even: 4
// → [20, 40]
Try It Yourself

How It Works

  1. Filter. R.filter keeps [2, 4].
  2. Peek. The curried R.forEach(...) logs each even number, then passes [2, 4] along unchanged.
  3. Map. R.map receives that same list and returns [20, 40].
  4. Native would break it. A step that returns undefined would make R.map fail.

Example 3 — Numbered List With R.addIndex

Get the position as well as the item.

Ramda
const forEachIndexed = R.addIndex(R.forEach);

forEachIndexed(
  (name, i) => console.log(`${i + 1}. ${name}`),
  ["Ada", "Lin", "Bo"]
);
// 1. Ada
// 2. Lin
// 3. Bo
Try It Yourself

How It Works

  1. No index by default. Plain R.forEach passes only the value.
  2. Add it. R.addIndex(R.forEach) builds a version whose callback also gets i.
  3. Count from 1. i starts at 0, so i + 1 gives human-friendly numbers.
  4. Reusable. forEachIndexed works on any list from now on.

Example 4 — Looping Over an Object

R.forEach is for lists. Use R.forEachObjIndexed for keys and values.

Ramda
const settings = { theme: "dark", lang: "en" };

R.forEachObjIndexed(
  (value, key) => console.log(key + " = " + value),
  settings
);
// theme = dark
// lang = en
Try It Yourself

How It Works

  1. Plain object. It has no length, so R.forEach(fn, settings) would quietly do nothing.
  2. Object version. R.forEachObjIndexed visits each own key.
  3. Value first. The callback gets (value, key), in the same order as Ramda’s other object helpers.
  4. Returns the object. Like R.forEach, it hands back the original settings.

Example 5 — forEach vs map

Returning a value from the callback does nothing in forEach.

Ramda
const nums = [1, 2, 3];
const double = n => n * 2;

const a = R.forEach(double, nums);
const b = R.map(double, nums);

console.log(a);           // → [1, 2, 3]
console.log(b);           // → [2, 4, 6]
console.log(a === nums);  // → true
Try It Yourself

How It Works

  1. forEach ignores results. double returns 2, 4, and 6, but R.forEach throws those values away.
  2. Same list back. a is nums itself, which is why a === nums is true.
  3. map collects results. R.map builds a new array from each return value: [2, 4, 6].
  4. Rule. Use map to compute values and forEach to do things.

Browser & Runtime Support

R.forEach has been a Ramda List helper since v0.1.1. It uses its own loop for arrays, so it runs anywhere Ramda runs. In the browser, load it from a CDN or a bundler. In Node.js, Bun, or Deno, run npm install ramda.

✓ Stable · Ramda 0.1.1+

R.forEach()

Support depends on the Ramda version you load, not on a browser feature. Your callback must also be valid in that runtime. For example, arrow functions and template literals need an ES2015 engine.

100% Modern browsers & runtimes
Google Chrome Modern versions · Desktop & Mobile
Full support
Mozilla Firefox Modern versions · Desktop & Mobile
Full support
Apple Safari Modern versions · macOS & iOS
Full support
Microsoft Edge Chromium-based
Full support
Opera Modern versions
Full support
Samsung Internet Modern versions · Android
Full support
Android WebView Modern WebView
Full support
Internet Explorer Not a target for current Ramda releases
Legacy
Node.js Maintained LTS · npm install ramda
Supported
Bun import from "ramda"
Supported
Deno import from "npm:ramda"
Supported
R.forEach Ramda 0.1.1+

Bottom line: Safe for production in any modern browser or JavaScript runtime once Ramda is loaded. The labs on this page use [email protected] from a CDN.

Frequently Asked Questions

R.forEach(fn, list) calls fn once for each item, from left to right, and returns the original list. It is meant for side effects such as logging or updating the page. Docs sample: R.forEach(x => console.log(x + 5), [1, 2, 3]) logs 6, 7, 8 and returns [1, 2, 3].
Native forEach returns undefined and skips holes in sparse arrays. R.forEach returns the same list you passed in, visits holes as undefined, is curried, and gives the callback only the value (no index).
No. Like the native method, it always visits every item unless the callback throws. Use R.find, R.any, or R.takeWhile when you want to stop at a condition, or a plain for...of loop with break.
Wrap it with R.addIndex: R.addIndex(R.forEach)((item, i) => console.log(i, item), list). Plain R.forEach passes only the value.
Use R.forEachObjIndexed((value, key) => ..., obj). R.forEach does nothing on a plain object because it has no length.
No. R.forEach has been part of Ramda since v0.1.1 and is stable. It is not deprecated, experimental, or non-standard.

Next: R.fromPairs()

Learn how to build an object from a list of key-value pairs.

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