Underscore.js _.wrap() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Function

Definition

Wraps the first function inside of the wrapper function, passing it as the first argument. This lets the wrapper run code before and after, adjust arguments, or execute conditionally (official Underscore docs). Stable Function helper — not deprecated.

Pass the required arguments shown below. Underscore function helpers usually return a new function — check timing and call-count behavior carefully.

Syntax

Call _.wrap. Start with this line.

Underscore
_.wrap(function, wrapper)
CallWhat it does
_.wrap(function, wrapper)Primary signature.
_.wrap(...)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
_.wrap(function, wrapper)

Parameters

ParameterDescription
functionRequired. Original function to wrap
wrapperRequired. Called as (func, …args)

Return Value

Return ValueDescription
*New wrapped function

Underscore.js Examples

Example 1 — Official Before / After

Wrap hello so a call builds a before/after string around moe.

Underscore
var hello = function (name) {
  return "hello: " + name;
};

hello = _.wrap(hello, function (func) {
  return "before, " + func("moe") + ", after";
});

hello();
// → "before, hello: moe, after"
Try It Yourself

How It Works

  1. Wrap. _.wrap returns a new hello.
  2. Call. hello() runs the wrapper, not the original directly.
  3. Inner call. Wrapper calls func("moe") → "hello: moe".
  4. Docs sample. This is the official Underscore example.

Example 2 — Forward Call Arguments

Use the arguments the caller passed after the original function.

Underscore
var greet = function (name) {
  return "hi " + name;
};

var loud = _.wrap(greet, function (func, name) {
  return func(name).toUpperCase();
});

loud("ada");
// → "HI ADA"
Try It Yourself

How It Works

  1. Signature. Wrapper is (func, name) — name comes from the call.
  2. Forward. func(name) runs the original with that argument.
  3. Decorate. Uppercase the return value before handing it back.
  4. vs official. Here args are not hardcoded — they come from the caller.

Example 3 — Call Conditionally

Skip the original when a guard fails.

Underscore
var double = function (n) { return n * 2; };

var safeDouble = _.wrap(double, function (func, n) {
  if (typeof n !== "number") {
    return "need a number";
  }
  return func(n);
});

safeDouble(5);      // → 10
safeDouble("x");    // → "need a number"
Try It Yourself

How It Works

  1. Guard. Check the argument before calling func.
  2. Pass. Numbers go through → func(5) returns 10.
  3. Skip. Non-numbers never reach double.
  4. Power. This is why wrap beats a fixed compose pipeline.

Example 4 — Log Around a Call

Run side effects before and after, and still return the original result.

Underscore
var add = function (a, b) { return a + b; };
var steps = [];

var loggedAdd = _.wrap(add, function (func, a, b) {
  steps.push("call " + a + "+" + b);
  var result = func(a, b);
  steps.push("got " + result);
  return result;
});

loggedAdd(2, 3);
// → 5
// steps → ["call 2+3", "got 5"]
Try It Yourself

How It Works

  1. Before. Push a “call …” note.
  2. Invoke. func(a, b) runs the real add.
  3. After. Record the result, then return it unchanged.
  4. Pattern. Classic decorate-without-editing the original.

Browser & Runtime Support

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

_.wrap()

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

_.wrap(function, wrapper) returns a new function. When called, Underscore runs your wrapper and passes the original function as the first argument, then any call arguments. Official example wraps hello to return "before, hello: moe, after".
As wrapper(func, ...args). The first parameter is the original function; the rest are the arguments you called the wrapped function with. You call func yourself inside the wrapper.
To decorate a function without editing it — add logging, timing, guards, or transform args/return values — while keeping the original reusable.
_.compose builds a fixed pipeline of return values. _.wrap gives the wrapper full control: if, when, and how to call the original.
The wrapper is invoked with the same this as the wrapped call. If you need a fixed this, bind with _.bind first.
Yes. You decide whether to call func at all — useful for guards and feature flags.

Next: _.negate()

Create a new predicate that returns the opposite of the original.

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