Creates a function that provides value to wrapper as its first argument. Extra arguments you pass to the created function are appended after that. Part of Lodash 4.x Function.
Note: The wrapper controls the call — you decide whether to invoke a wrapped function (like _.escape) and how to shape the return value.
Syntax
Call _.wrap. Start with this line.
Lodash
_.wrap(value, wrapper)
Call
What it does
_.wrap(value, wrapper)
Primary signature.
_.wrap(func)
Wrap or transform a function.
Prefer import wrap from "lodash/wrap" when you want a smaller bundle.
Library declarations
Lodash
_.wrap(value, wrapper)
Parameters
Parameter
Description
value
Required. Passed as first arg to wrapper
wrapper
Required. Function invoked on each call
Return Value
Return Value
Description
*
New function
Lodash Examples
Example 1 — Official: Escape Inside <p>
Decorate _.escape so the safe string is wrapped in a paragraph.
Lodash
var p = _.wrap(_.escape, function (func, text) {
return '<p>' + func(text) + '</p>';
});
p('fred, barney, & pebbles');
// → '<p>fred, barney, & pebbles</p>'
Result. Simple concatenation — same API shape as wrapping a function.
Example 3 — Log Around a Function
Call the inner function and annotate the result.
Lodash
var double = function (n) { return n * 2; };
var traced = _.wrap(double, function (func, n) {
var result = func(n);
return 'in=' + n + ' out=' + result;
});
traced(3);
// → 'in=3 out=6'
Pattern. Same idea as logging, timing, or caching wrappers.
Example 4 — Skip the Inner Call
The wrapper may ignore the wrapped value entirely.
Lodash
var risky = function () { throw new Error('boom'); };
var safe = _.wrap(risky, function (func, flag) {
if (!flag) return 'skipped';
return func();
});
safe(false);
// → 'skipped'
// safe(true) would throw
Power. Wrap is an interceptor, not a forced forwarder.
Example 5 — wrap vs partial
Partial fixes args; wrap intercepts the whole call.
Lodash
function join(a, b) { return a + '-' + b; }
var withPartial = _.partial(join, 'pre');
withPartial('x');
// → 'pre-x'
var withWrap = _.wrap(join, function (func, b) {
return '[' + func('pre', b) + ']';
});
withWrap('x');
// → '[pre-x]'
partial. Locks 'pre' as the first argument of join.
wrap. Receives join itself and builds a decorated return.
Both useful. Fix args → partial; decorate/intercept → wrap.
Rule. If you only need bound args, prefer partial.
Compatibility
Browser & Runtime Support
_.wrap is a Lodash 4.x Function helper. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.
✓ Stable · Lodash 4.x
_.wrap()
Available in [email protected] full builds and the lodash/wrap modular import. Closest plain-JS pattern is (value) => (...args) => wrapper(value, ...args).
100%With Lodash 4.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
_.wrapLodash 4.x+
Bottom line: Safe for production once Lodash is loaded. Prefer import wrap from "lodash/wrap" for smaller bundles. Remember: the wrapper controls whether the wrapped value/function runs.
Frequently Asked Questions
_.wrap(value, wrapper) returns a new function. When you call it, Lodash invokes wrapper with value as the first argument, then any arguments you passed to the outer function.
Always the wrapped value — often a function like _.escape. Call it yourself when you want the original behavior to run.
partial fixes leading arguments ahead of time. wrap intercepts every call: you decide whether and how to use value and the caller args.
Yes. Any value works — a string, number, or object is passed as the first wrapper argument. Wrapping functions is the most common pattern.
The wrapper is invoked with the this binding of the created function. Bind separately when method context matters.
Use import wrap from "lodash/wrap" or require("lodash/wrap") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.wrap.