Lodash _.forEachRight() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.forEachRight(collection, iteratee) differs only in traversal direction from _.forEach.
  • Tail-first patterns for stacked undo buffers, trailing-sentinel scans, or preserving last-writer expectations.
  • When manual reverse duplicates work—and when Lodash keeps iteratee ergonomics simpler.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Read _.forEach() first—the iteratee signature and return semantics are identical.

  • You can reason about zero-based indexes from the end of an array.
  • You can open Try-it labs or run snippets locally.

Overview

_.forEachRight is the mirrored cousin of _.forEach: same chaining story, same iteratee toolbox—only the visitation schedule runs backward on indexed collections.

Tail-first arrays

Great when the meaningful signal lives near the end—audit trails, version stacks, append logs.

Chain-ready

Returns the collection so fluent pipelines stay uninterrupted.

Exit friendly

Return false to bail once a backward scan finds what it needs.

Syntax

javascript
_.forEachRight(collection, iteratee)
  • collection: array, array-like, or plain object Lodash can iterate.
  • iteratee: invoked as (value, index|key, collection); indexes descend on arrays.
  • Returns: the original collection reference.
1

Capture visitation indexes from the tail

Push each visited index into an array—the descending pattern becomes obvious compared with forEach.

javascript
import forEachRight from "lodash/forEachRight";

const seen = [];
forEachRight(["a", "b", "c"], (_value, index) => {
  seen.push(index);
});
seen;
// → [2, 1, 0]
Try it Yourself
2

Concatenate values tail-first

String-building shows how later slots dominate ordering without reversing the source array.

javascript
import forEachRight from "lodash/forEachRight";

let acc = "";
forEachRight([10, 20, 30], (n) => {
  acc += String(n);
});
acc;
// → "302010"
Try it Yourself
3

Stop after two visits from the right

return false still halts the loop—here only the last two elements run before exit.

javascript
import forEachRight from "lodash/forEachRight";

const hits = [];
forEachRight([10, 5, 20], (value) => {
  hits.push(value);
  if (hits.length === 2) return false;
});
hits;
// → [20, 5]
Try it Yourself

📋 _.forEachRight vs forEach, reverse, findLast

APITraversalBest when
_.forEachRight(collection, iteratee)Tail → head on arraysEffects must observe trailing elements first
_.forEach(collection, iteratee)Head → tailDefault forward scans
array.slice().reverse().forEach(...)Reversed copyWhen you explicitly need a reversed snapshot
_.findLast(...)First match from tailYou only need one element, not every callback

Pitfalls to avoid

Objects

Key order myths

Do not assume object keys mirror array reverse semantics—verify against fixtures.

Async

Still not async-aware

Await inside iteratee does not serialize magically—use explicit async flows.

Perf

Accidental O(n) work

If only the last element matters, findLast may avoid redundant callbacks.

❓ FAQ

The original collection reference—the same chaining affordance as _.forEach.
For arrays and array-like values Lodash starts at the last index and walks backward.
Yes—explicit false mirrors _.forEach early-exit semantics.
Avoids allocating a reversed copy and keeps Lodash iteratee shorthands consistent across collection types.
Treat key order as Lodash-defined—verify against docs or fixtures when order matters.

Summary

  • Purpose: _.forEachRight(collection, iteratee) runs side-effect callbacks from the end on indexed collections.
  • Contrast: prefer forEach when forward order reads clearer; prefer findLast when one hit suffices.
  • Next: Lodash _.groupBy(), Lodash _.forEach() (previous), or collection hub.
Did you know?

For arrays, indexes decrease from length - 1 down to 0. For plain objects, rely on Lodash’s documented enumeration rather than assuming insertion-order symmetry with forEach.

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