Lodash _.forEach() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Collection

Definition

Invokes an iteratee for each element in a collection (array or object). Callbacks receive (value, index|key, collection). Return false to stop early. Alias: _.each. Part of Lodash 4.x Collection.

Pass the required arguments shown below. Lodash Collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.forEach on a collection. Start with this line.

Lodash
_.forEach(collection, [iteratee=_.identity])
CallWhat it does
_.forEach(collection, [iteratee=_.identity])Primary signature.
_.forEach(collection)Call with a collection argument.

Prefer import forEach from "lodash/forEach" when you want a smaller bundle.

Library declarations
Lodash
_.forEach(collection, [iteratee=_.identity])

Parameters

ParameterDescription
collectionRequired. Array or object to iterate
iterateeOptional. Called per element; default _.identity

Return Value

Return ValueDescription
*The original collection

Lodash Examples

Example 1 — Official: Array Values

Visit each number left to right.

Lodash
var logged = [];

_.forEach([1, 2], function (value) {
  logged.push(value);
});

// logged → [1, 2]
Try It Yourself

How It Works

  1. Source. Two-element array indexes 0 and 1.
  2. First call. Iteratee receives 1.
  3. Second call. Iteratee receives 2.
  4. Return. _.forEach itself returns the original array.

Example 2 — Official: Object Keys

On objects, the second argument is the key.

Lodash
var keys = [];

_.forEach({ a: 1, b: 2 }, function (value, key) {
  keys.push(key);
});

// keys → ["a", "b"]
Try It Yourself

How It Works

  1. Values first. Callback still gets the property value as arg 1.
  2. Keys second. Arg 2 is "a", then "b".
  3. Order. Modern engines usually follow insertion order; do not rely on exotic key orders.
  4. Tip. Need inherited keys? Prefer _.forIn.

Example 3 — Accumulate Outside the Callback

Classic side-effect pattern: update a total as you walk.

Lodash
var total = 0;

_.forEach([12, 7, 30], function (n) {
  total += n;
});

// total → 49
Try It Yourself

How It Works

  1. Seed. total starts at 0 outside forEach.
  2. Add. Each visit adds the current number.
  3. Done. After three visits: 12 + 7 + 30 = 49.
  4. Alt. For pure reduction, prefer _.reduce — clearer intent.

Example 4 — Early Exit With false

Return false to stop — a Lodash advantage over native forEach.

Lodash
var hits = [];

_.forEach([5, 40, 99, 12], function (value) {
  hits.push(value);
  if (value > 50) {
    return false; // stop
  }
});

// hits → [5, 40, 99]   // 12 never visited
Try It Yourself

How It Works

  1. Push first. Each visited value is recorded.
  2. Check. When 99 > 50, return false.
  3. Stop. Lodash ends the loop — 12 is skipped.
  4. Only false. Other falsy returns (0, "") do not stop — must be boolean false.

Example 5 — Alias _.each

Same helper under a shorter name.

Lodash
var out = [];

_.each(["x", "y"], function (ch, i) {
  out.push(i + ":" + ch);
});

// out → ["0:x", "1:y"]
Try It Yourself

How It Works

  1. Alias. _.each === _.forEach in Lodash builds.
  2. Index. Second arg is the array index.
  3. Format. Each visit builds "index:value".
  4. Style. Prefer one name in a codebase for consistency.

Browser & Runtime Support

_.forEach is a Lodash 4.x Collection helper (alias _.each). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native Array.prototype.forEach is ES5 but cannot early-exit with false.

✓ Stable · Lodash 4.x

_.forEach()

Available in [email protected] full builds and the lodash/forEach modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
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
_.forEach Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import forEach from "lodash/forEach" for smaller builds. Use Lodash when you need early exit or object collections.

Frequently Asked Questions

_.forEach(collection, iteratee) calls iteratee once per element. Arguments are (value, index|key, collection). It returns the original collection.
Return false from the iteratee. Lodash then stops visiting further elements. Native Array.forEach cannot break this way.
Not by itself. Your callback might mutate elements or nested objects — keep side effects intentional.
Use forEach for side effects (log, accumulate outside, DOM). Use _.map when you need a new array of transformed values.
_.each is an alias of _.forEach — same behavior.
Lodash may treat them like arrays. For own enumerable keys only, prefer _.forOwn or _.forIn.

Next: _.forEachRight()

Iterate from the end toward the start — useful for stacks and reverse passes.

_.forEachRight() 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.

11 people found this page helpful