Underscore.js _.iteratee() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Utility

Definition

Generates a callback that can be applied to each element in a collection (official Underscore docs). Supports shorthand syntaxes for common callback use cases.

Pass the required arguments shown below. Underscore utility helpers usually return a new value or function — check edge cases before you ship.

Syntax

Call _.iteratee. Start with this line.

Underscore
_.iteratee(value, [context])
CallWhat it does
_.iteratee(value, [context])Primary signature.
_.iteratee(...)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
_.iteratee(value, [context])

Parameters

ParameterDescription
valueOptional. Null → identity; fn / object / other → shorthand
contextOptional. this binding when value is a function

Return Value

Return ValueDescription
*A callback function

Underscore.js Examples

Example 1 — Official Four Shorthands

Null, function, object, and string each become a usable callback.

Underscore
var person = { firstName: 'Chelsea', age: 30 };

_.iteratee()(person) === person;                    // true
_.iteratee(function (n) { return n * 2; })(4);      // 8
_.iteratee({ firstName: 'Chelsea' })(person);       // true
_.iteratee('firstName')(person);                    // "Chelsea"
Try It Yourself

How It Works

  1. No value. Returns _.identity — same object back.
  2. Function. Your function is used as-is (4 → 8).
  3. Object. Becomes a matcher for those key/value pairs.
  4. String. Becomes a property reader for firstName.

Example 2 — Property Shorthand with Map

_.map already runs the iteratee — pass a property name directly.

Underscore
var people = [
  { firstName: 'Chelsea' },
  { firstName: 'Moe' }
];

_.map(people, 'firstName');
// → ['Chelsea', 'Moe']

// Same as:
_.map(people, _.iteratee('firstName'));
Try It Yourself

How It Works

  1. Pass a string. 'firstName' is not a function.
  2. Iteratee converts it. Underscore builds _.property('firstName').
  3. Map applies it. Each object yields its firstName.
  4. Shorthand win. No need to write function (p) { return p.firstName; }.

Example 3 — Matcher Shorthand with Filter

A plain object predicate keeps items that contain those properties.

Underscore
var list = [
  { firstName: 'Chelsea', active: true },
  { firstName: 'Moe', active: false },
  { firstName: 'Larry', active: true }
];

_.filter(list, { active: true });
// → [{ firstName: 'Chelsea', active: true },
//    { firstName: 'Larry', active: true }]
Try It Yourself

How It Works

  1. Pass an object. { active: true } is a matcher shorthand.
  2. Iteratee wraps it. Becomes _.matcher({ active: true }).
  3. Filter keeps matches. Only active people remain.
  4. Same idea. Works in find, reject, where-style checks.

Example 4 — Official RegExp Override

Overwrite _.iteratee so a RegExp becomes a .test predicate.

Underscore
var builtinIteratee = _.iteratee;
_.iteratee = function (value, context) {
  if (_.isRegExp(value)) {
    return function (obj) { return value.test(obj); };
  }
  return builtinIteratee(value, context);
};

_.filter(['apple', 'pie', 'apply'], /app/);
// → ['apple', 'apply']
Try It Yourself

How It Works

  1. Save the built-in. Keep builtinIteratee for other types.
  2. Detect RegExp. Return a function that calls value.test(obj).
  3. Fall through. Everything else uses the original iteratee.
  4. Filter picks it up. /app/ now works as a shorthand predicate.

Browser & Runtime Support

_.iteratee ships with Underscore.js since 1.7.0. It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.7.0+

_.iteratee()

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

100% With Underscore 1.7.0+
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
_.iteratee Underscore 1.7.0+

Bottom line: Safe for production once Underscore ≥ 1.7.0 is loaded. Not a native browser API. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.iteratee(value, [context]) generates a callback for collection methods. No value → _.identity. A function is returned (bound if you pass context). A plain object → _.matcher. Anything else (such as a property name) → _.property.
Four shorthands: _.iteratee() is identity, _.iteratee(function(n){ return n * 2; }) is that function, _.iteratee({firstName: 'Chelsea'}) is a matcher, and _.iteratee('firstName') is a property accessor.
Usually no. Methods such as map, filter, find, and groupBy already run their predicates through iteratee. Call it yourself for a reusable callback, or when you overwrite iteratee to add a shorthand.
countBy, every, filter, find, findIndex, findKey, findLastIndex, groupBy, indexBy, map, mapObject, max, min, partition, reject, some, sortBy, sortedIndex, and uniq.
Yes. Save the built-in, then assign a new function that handles extra types (such as RegExp) and otherwise calls the original. That change is global for that Underscore object.
No. It is a stable Underscore utility (since 1.7.0) that customizes the iterators used by collection functions.

Next: _.uniqueId()

Generate unique ids for client-side models or DOM elements that need one.

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