Lodash _.wrap() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

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)
CallWhat 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

ParameterDescription
valueRequired. Passed as first arg to wrapper
wrapperRequired. Function invoked on each call

Return Value

Return ValueDescription
*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, &amp; pebbles</p>'
Try It Yourself

How It Works

  1. value. _.escape is stored as the wrapped value.
  2. Call. p(text) runs the wrapper with (_.escape, text).
  3. Escape. & becomes &amp; for safe HTML.
  4. Decorate. Wrapper adds <p>…</p> around the result.

Example 2 — Wrap a Non-Function Value

Any value works — here a greeting prefix string.

Lodash
var greet = _.wrap('hello', function (prefix, name) {
  return prefix + ' ' + name;
});

greet('world');
// → 'hello world'
Try It Yourself

How It Works

  1. value. The string 'hello' is not a function.
  2. First arg. Wrapper receives it as prefix.
  3. Caller arg. 'world' becomes name.
  4. 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'
Try It Yourself

How It Works

  1. Inner. double is the wrapped function.
  2. Call through. Wrapper runs func(n) itself.
  3. Annotate. Builds a string with input and output.
  4. 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
Try It Yourself

How It Works

  1. Control. Wrapper decides whether to call func.
  2. flag false. Returns early — inner never runs.
  3. flag true. Would invoke risky and throw.
  4. 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]'
Try It Yourself

How It Works

  1. partial. Locks 'pre' as the first argument of join.
  2. wrap. Receives join itself and builds a decorated return.
  3. Both useful. Fix args → partial; decorate/intercept → wrap.
  4. Rule. If you only need bound args, prefer partial.

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 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 Lodash 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.

Next: _.now()

Get the number of milliseconds since the Unix epoch.

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