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)
Call
What 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
Parameter
Description
function
Required. Original function to wrap
wrapper
Required. Called as (func, …args)
Return Value
Return Value
Description
*
New wrapped function
Underscore.js Examples
Example 1 — Official Before / After
Wrap hello so a call builds a before/after string around moe.
Signature. Wrapper is (func, name) — name comes from the call.
Forward.func(name) runs the original with that argument.
Decorate. Uppercase the return value before handing it back.
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"
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"]
After. Record the result, then return it unchanged.
Pattern. Classic decorate-without-editing the original.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.wrapUnderscore 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.