Underscore.js _.mixin() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Utility

Definition

Allows you to extend Underscore with your own utility functions (official Underscore docs). Pass a hash of {name: function} definitions to add them to the Underscore object and the OOP wrapper. Returns _ to facilitate chaining.

Pass the required arguments shown below. Underscore utility helpers usually return a new value or function — check edge cases before you ship.

Syntax

Call _.mixin. Start with this line.

Underscore
_.mixin(object)
CallWhat it does
_.mixin(object)Primary signature.
_.mixin(...)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
_.mixin(object)

Parameters

ParameterDescription
objectRequired. Hash of function names → implementations

Return Value

Return ValueDescription
*The Underscore object _

Underscore.js Examples

Example 1 — Official Capitalize

Mix in a helper, then call it on the OOP wrapper.

Underscore
_.mixin({
  capitalize: function (string) {
    return string.charAt(0).toUpperCase()
         + string.substring(1).toLowerCase();
  }
});

_("fabio").capitalize();
// → "Fabio"
Try It Yourself

How It Works

  1. Define the helper. capitalize uppercases the first letter.
  2. Mix it in. Underscore copies it to _ and the wrapper.
  3. Wrap a string. _("fabio") holds the value.
  4. Call the method. Wrapper passes "fabio" as the first argument → "Fabio".

Example 2 — Static Call and OOP Call

After mixin, both styles work the same.

Underscore
_.mixin({
  shout: function (s) {
    return String(s).toUpperCase() + "!";
  }
});

_.shout("hi");       // → "HI!"
_("hi").shout();     // → "HI!"
Try It Yourself

How It Works

  1. Mix once. shout lands on _ and _.prototype.
  2. Static style. Pass the string yourself: _.shout("hi").
  3. OOP style. Wrap first; the value is prepended automatically.
  4. Same result. Both return "HI!".

Example 3 — Extra Arguments on the Wrapper

Extra args after the method name follow the wrapped value.

Underscore
_.mixin({
  repeat: function (s, n) {
    return Array(n + 1).join(s);
  }
});

_("ha").repeat(3);
// → "hahaha"

// Same as: _.repeat("ha", 3)
Try It Yourself

How It Works

  1. Define two params. s is the string; n is the count.
  2. Wrap the string. _("ha") becomes the first argument.
  3. Pass the rest. .repeat(3) supplies n.
  4. Build the result. Join "ha" three times → "hahaha".

Example 4 — Use a Mixin with _.map

Once mixed in, your helper is a normal Underscore function — pass it to collection methods.

Underscore
_.mixin({
  capitalize: function (string) {
    return string.charAt(0).toUpperCase()
         + string.substring(1).toLowerCase();
  }
});

_.map(["ada", "GRACE"], _.capitalize);
// → ["Ada", "Grace"]
Try It Yourself

How It Works

  1. Mix in capitalize. It becomes _.capitalize.
  2. Pass it to map. Each name is transformed.
  3. Normalize case. "ada" → "Ada", "GRACE" → "Grace".
  4. Pattern. Custom helpers plug into the rest of Underscore.

Browser & Runtime Support

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

_.mixin()

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

_.mixin(object) copies each function in the object onto Underscore and onto the OOP wrapper. After mixin, you can call _.yourFn(value) and _(value).yourFn().
_.mixin({ capitalize: function(string) { return string.charAt(0).toUpperCase() + string.substring(1).toLowerCase(); } }) then _('fabio').capitalize() returns 'Fabio'.
Yes. It returns the Underscore object itself so you can keep chaining calls on _. That is what the docs mean by facilitating chaining.
The wrapped value is prepended as the first argument. _('hi').shout(3) is the same as _.shout('hi', 3).
Yes. If you mix in a function named map, it replaces _.map. Prefer unique names, and avoid mixing into a shared global _ in large apps.
No. It is a stable Underscore utility so you can extend Underscore with your own functions.

Next: _.iteratee()

Learn how Underscore turns null, functions, objects, and property names into collection callbacks.

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