Lodash _.wrap() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How the wrapper’s first argument is the wrapped function reference.
  • Decorating behavior—sanitize HTML then wrap it in markup.
  • Forwarding spread arguments through transparent decorators.
  • When partial / bind fits better than wrap.

Prerequisites

Comfortable with _.partial(), _.bind(), and the Function hub.

  • Higher-order functions: functions that accept or return other functions.
  • Rest parameters: (fn, ...args) => fn(...args) forwarding patterns.

Overview

_.wrap(func, wrapper) builds a callable that forwards outer arguments into wrapper, inserting func as the first parameter—classic decorator ergonomics without rewriting func itself.

Syntax

javascript
_.wrap(func, wrapper)
  • func: callable Lodash wraps and exposes as the wrapper’s first argument.
  • wrapper: invoked as wrapper(func, ...args) with same this as the outer invocation unless otherwise fixed.
  • returns: new function applying that delegation rule.
1

Escape text, then add markup

Lodash’s cookbook pattern—delegate sanitizing to escape, then wrap the safe string.

javascript
import wrap from "lodash/wrap";
import escape from "lodash/escape";

const paragraph = wrap(escape, (fn, text) => `

${fn(text)}

`); paragraph("Tom & Jerry"); // "

Tom & Jerry

"
2

Transform the inner result

Call through then reshape output—independent of how many arguments the wrapped API expects.

javascript
import wrap from "lodash/wrap";

function greet(name) {
  return `Hello, ${name}!`;
}

const shout = wrap(greet, (fn, name) => fn(name).toUpperCase());

shout("Ada");
// "HELLO, ADA!"
3

Transparent proxies

Intercept arbitrary arity—summarize inputs without rewriting every caller.

javascript
import wrap from "lodash/wrap";

function sum(a, b) {
  return a + b;
}

const audited = wrap(sum, (fn, ...args) => {
  const logged = args.reduce((acc, n) => acc + n, 0);
  const inner = fn(...args);
  return { loggedSumHint: logged, result: inner };
});

audited(2, 3);
// { loggedSumHint: 5, result: 5 }

📋 _.wrap vs _.partial vs _.bind

ToolUse whenSignature intuition
_.wrapAdd behavior around each invocation(fn, ...outer) → wrapper controls calling fn
_.partialFreeze leading arguments ahead of timePartially applied curried slots
_.bindPin this or mix placeholdersBound receiver + partial args

Pitfalls to avoid

Recursion

Calling the wrong fn reference

Pass along Lodash’s injected callable—the closure capturing stale refs risks recursion surprises.

this

Methods on prototypes

Extract methods safely (bind, bound arrows) before wrapping when receivers carry hidden state.

Typing

Inference friction

Type declarations seldom encode Lodash’s injected-first-arg convention—document wrapper contracts by hand.

❓ FAQ

A new function that, when called, invokes wrapper with func as the first argument and passes along the outer caller's arguments afterward.
The original wrapped function—call fn(...args) when you want the wrapped behavior to run.
Partial fixes arguments ahead of time. Wrap wraps invocation entirely—you intercept each call and decide how to forward args.
The outer wrapper forwards this like an ordinary function wrapper—combine with bind when receiver-sensitive methods matter.
Use import wrap from "lodash/wrap" for tree-shaking friendly bundles.

Summary

Did you know?

Lodash passes the wrapped function reference as the first argument to your wrapper, followed by whatever arguments the outer caller supplied—so the wrapper controls whether and how func runs.

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.

6 people found this page helpful