Lodash _.forEach() method

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

What you’ll learn

  • How _.forEach(collection, iteratee) walks arrays and objects with a stable callback signature.
  • Why the method returns the collection and how that enables chaining.
  • When to prefer map/reduce instead of mutating outer state inside the iteratee.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Review _.filter() or the collection hub when you need transformed data instead of imperative walks.

  • You understand callbacks as (value, index|key, collection).
  • You can open Try-it labs or run snippets locally.

Overview

_.forEach is the readable bridge between imperative loops and Lodash pipelines—especially when you must touch Maps-like plain objects or keep fluent chains alive.

Uniform walks

One helper covers indexed lists and keyed dictionaries without branching boilerplate.

Chain-friendly

Returning the collection mirrors fluent APIs even after side-effect steps.

Escape hatch

Return false when continuing would waste work—document the sentinel for teammates.

Syntax

javascript
_.forEach(collection, iteratee)
  • collection: array, array-like, or plain object Lodash can iterate.
  • iteratee: invoked as (value, index|key, collection); returning false stops further iteration.
  • Returns: the original collection reference.
1

Accumulate with an outer binding

Prefer reduce when the aggregation is pure functional style—this pattern shows the imperative accumulator Lodash still enables cleanly.

javascript
import forEach from "lodash/forEach";

let total = 0;
forEach([12, 7, 30], (n) => {
  total += n;
});
// total → 49
Try it Yourself
2

Walk string-keyed objects

Callbacks receive both value and key—ideal for formatting configs or emitting diagnostics.

javascript
import forEach from "lodash/forEach";

const lines = [];
forEach({ host: "edge", region: "iad" }, (value, key) => {
  lines.push(`${key}=${value}`);
});
lines.join(",");
// → "host=edge,region=iad"
Try it Yourself
3

Stop early by returning false

Halts remaining visits once a sentinel condition fires—use sparingly and comment why.

javascript
import forEach from "lodash/forEach";

const hits = [];
forEach([5, 40, 99, 12], (value) => {
  hits.push(value);
  if (value > 50) return false;
});
hits;
// → [5, 40, 99]
Try it Yourself

📋 _.forEach vs map, each, native forEach

APIPrimary goalBest when
_.forEach(collection, iteratee)Effects / readsUniform iteration across arrays and objects
_.map(collection, iteratee)New transformed arrayPure projections without manual pushes
_.eachAlias of forEachLegacy code style parity
array.forEach(fn)EffectsArrays only—no object dictionary walks

Pitfalls to avoid

Async

Non-async friendly

forEach does not await promises—use explicit loops or reduce chains with async orchestration.

Returns

Expecting an array

Returning arbitrary values from normal iterations does nothing—switch to map when collecting outputs.

Mutation

Hidden writes

Mutating shared outer scope can surprise readers—keep callbacks tight or prefer reducers.

❓ FAQ

The original collection reference—handy for chaining; use map or reduce when you need a transformed value.
Not by itself—your iteratee might mutate elements or nested objects, so keep callbacks disciplined.
Returning false from the iteratee ends the loop for lodash collection iteration (see Lodash docs for edge cases with external iteratees).
map builds a new array of results; forEach runs for effects or reads and returns the input collection.
Yes—callbacks receive (value, key, collection) while Lodash walks enumerable string keys.

Summary

Did you know?

Returning false from the iteratee tells Lodash to stop visiting further entries—document that sentinel so teammates do not confuse it with ordinary falsy callback results.

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