Underscore.js _.each() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Iterates over a list of elements, yielding each in turn to an iteratee function. Alias _.forEach. Returns the list for chaining (official Underscore docs). Stable Collection helper — not deprecated.

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

Syntax

Call _.each. Start with this line.

Underscore
_.each(list, iteratee, [context])
CallWhat it does
_.each(list, iteratee, [context])Primary signature.
_.each(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.each(list, iteratee, [context])

Parameters

ParameterDescription
listRequired. Array, array-like, or plain object
iterateeRequired. Called once per entry
contextOptional. Bound as this in the iteratee

Return Value

Return ValueDescription
*The original list (for chaining)

Underscore.js Examples

Example 1 — Official Array Iteration

Visit every number in turn (docs use alert; we collect for clarity).

Underscore
var seen = [];

_.each([1, 2, 3], function (n) {
  seen.push(n);
});

seen;
// → [1, 2, 3]
Try It Yourself

How It Works

  1. Walk. Underscore calls the iteratee for 1, then 2, then 3.
  2. Args. Each call is (element, index, list).
  3. Side effect. We push into seen — classic _.each use.
  4. Return. _.each itself returns the original array.

Example 2 — Official Object Iteration

Plain objects yield values (and keys) — not just arrays.

Underscore
var pairs = [];

_.each({ one: 1, two: 2, three: 3 }, function (value, key) {
  pairs.push(key + "=" + value);
});

pairs;
// → ["one=1", "two=2", "three=3"]
Try It Yourself

How It Works

  1. Object mode. Iteratee args are (value, key, list).
  2. Value first. Unlike jQuery’s $.each(index, value).
  3. Own keys. Underscore walks the object’s own enumerable properties.
  4. Docs sample. Same collection as the official object demo.

Example 3 — Bind this with Context

Pass a third argument so methods use the right this.

Underscore
var logger = {
  prefix: "n=",
  lines: [],
  add: function (n) {
    this.lines.push(this.prefix + n);
  }
};

_.each([1, 2, 3], logger.add, logger);

logger.lines;
// → ["n=1", "n=2", "n=3"]
Try It Yourself

How It Works

  1. Method ref. logger.add alone would lose this.
  2. Context. Third arg binds this to logger.
  3. Calls. Each number is appended as "n=…".
  4. Alternative. _.each(list, function (n) { logger.add(n); }) also works.

Example 4 — Side Effects vs _.map

_.each for actions; _.map when you need a result array.

Underscore
var total = 0;
_.each([10, 20, 30], function (n) {
  total += n;
});
// total → 60

var doubled = _.map([10, 20, 30], function (n) {
  return n * 2;
});
// doubled → [20, 40, 60]

_.forEach === _.each;
// → true
Try It Yourself

How It Works

  1. each. Mutates outer total — a side effect.
  2. map. Returns a new array of transformed values.
  3. Alias. _.forEach is the same function as _.each.
  4. Rule. Prefer _.map when the return value matters.

Browser & Runtime Support

_.each (alias _.forEach) ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.each()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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
_.each Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.each(list, iteratee, [context]) walks every item in an array, array-like value, or plain object and calls your iteratee once per entry. It returns the original list for chaining.
Yes. forEach is an alias of each. They are the same function.
Arrays: (element, index, list). Plain objects: (value, key, list). The value comes first.
No. Underscore documents that an each loop cannot be broken out of. Use _.find (or another helper) to stop at the first match.
Optional third argument. Underscore binds the iteratee so this inside the callback refers to that object.
Use _.each for side effects (logging, pushing, DOM updates). Use _.map when you need a new array of transformed values.

Next: _.map()

Transform every item and collect the results into a new array.

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

10 people found this page helpful