Underscore.js _.tap() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Object

Definition

Invokes interceptor with the object, then returns that same object (official Underscore docs). Built to “tap into” a method chain and act on intermediate results. Stable Object helper — not deprecated.

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

Syntax

Call _.tap. Start with this line.

Underscore
_.tap(object, interceptor)
CallWhat it does
_.tap(object, interceptor)Primary signature.
_.tap(...)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
_.tap(object, interceptor)

Parameters

ParameterDescription
objectRequired. Value to pass through and return
interceptorRequired. Function called once with that value

Return Value

Return ValueDescription
*Same object (interceptor return ignored)

Underscore.js Examples

Example 1 — Basic Tap

The interceptor sees the value; _.tap returns that same value.

Underscore
var notes = [];
var result = _.tap([1, 2, 3], function (arr) {
  notes.push("saw " + arr.length + " items");
});

// notes → ["saw 3 items"]
// result → [1, 2, 3]  (same array)
Try It Yourself

How It Works

  1. Pass a value. Here an array [1, 2, 3].
  2. Run the interceptor. It records a note (side effect).
  3. Get the same array back. result is still [1, 2, 3].
  4. Intent. Peek without replacing the value.

Example 2 — Official: Filter, Tap, Map

Same shape as the docs — tap mid-chain so alert (or a log) does not break fluency.

Underscore
var tapped = null;

var squared = _.chain([1, 2, 3, 200])
  .filter(function (num) { return num % 2 === 0; })
  .tap(function (evens) { tapped = evens; })
  .map(function (num) { return num * num; })
  .value();

// tapped → [2, 200]
// squared → [4, 40000]
Try It Yourself

How It Works

  1. Filter. Keep even numbers → [2, 200].
  2. Tap. Interceptor stores that intermediate array.
  3. Map. Square each remaining number → [4, 40000].
  4. Why tap. A bare alert/console.log return would break the chain; .tap keeps the value flowing.

Example 3 — Interceptor Return Is Ignored

Returning a new value from the interceptor does not change what _.tap returns.

Underscore
var value = _.tap(10, function (n) {
  return n * 100;   // discarded
});

// value → 10  (not 1000)
Try It Yourself

How It Works

  1. Call tap. Underscore runs function (n) { return n * 100; }.
  2. Discard return. 1000 is never used by _.tap.
  3. Hand back input. You still get 10.
  4. To transform. Use _.map, another step, or assign outside tap — not the interceptor return.

Example 4 — Side Effect Mid-Pipeline

Record a metric while a mapped array continues through the chain.

Underscore
var log = [];

var doubled = _.chain([1, 2, 3])
  .map(function (n) { return n * 2; })
  .tap(function (arr) {
    log.push("mapped " + arr.length + " items");
  })
  .value();

// doubled → [2, 4, 6]
// log → ["mapped 3 items"]
Try It Yourself

How It Works

  1. Map first. [1,2,3] becomes [2,4,6].
  2. Tap next. Push a log line about the current array.
  3. Unwrap. .value() returns the mapped array unchanged.
  4. Pattern. Side effect in the middle; data keeps flowing.

Browser & Runtime Support

_.tap 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

_.tap()

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
_.tap 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

_.tap(object, interceptor) calls interceptor(object), then returns the same object. Use it to peek or run a side effect without replacing the value in a chain.
Tapping into a method chain so you can inspect or act on an intermediate result, then let the chain continue with the same value. See the official filter → tap → map example on underscorejs.org.
No. _.tap always returns the exact object you passed in. Changes happen only if your interceptor mutates that object.
It is ignored. Even if the interceptor returns something else, _.tap still hands back the original object.
Call .tap(fn) between steps. The wrapped value is passed to fn; the chain keeps flowing with that same value until .value().
No. _.each loops each element. _.tap hands the whole value once to the interceptor and returns it — it does not iterate.

Next: _.toPath()

Normalize strings and arrays into a path Underscore can use for deep gets and sets.

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