Lodash _.unary() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

Creates a function that accepts up to one argument, ignoring any additional arguments. Part of Lodash 4.x Function.

Note: Same idea as _.ary(func, 1) — use unary when you specifically want a one-argument adapter (classic fix for map(parseInt)).

Syntax

Call _.unary. Start with this line.

Lodash
_.unary(func)
CallWhat it does
_.unary(func)Primary signature.
_.unary(func)Wrap or transform a function.

Prefer import unary from "lodash/unary" when you want a smaller bundle.

Library declarations
Lodash
_.unary(func)

Parameters

ParameterDescription
funcRequired. Function to wrap

Return Value

Return ValueDescription
*New function (arity capped at 1)

Lodash Examples

Example 1 — Official: _.map + parseInt

Cap parseInt so map indexes never become a radix.

Lodash
_.map(['6', '8', '10'], _.unary(parseInt));
// → [6, 8, 10]
Try It Yourself

How It Works

  1. map. Would pass (value, index, array).
  2. unary. Forwards only value to parseInt.
  3. Default radix. Strings parse as decimal (modern engines).
  4. Result. Clean [6, 8, 10].

Example 2 — Broken Native map(parseInt)

Without unary, indexes become accidental radix values.

Lodash
['6', '8', '10'].map(parseInt);
// → [6, NaN, 2]   // index used as radix (JSON shows null for NaN)

_.map(['6', '8', '10'], _.unary(parseInt));
// → [6, 8, 10]
Try It Yourself

How It Works

  1. Index 1. parseInt('8', 1) is invalid radix → NaN.
  2. Index 2. parseInt('10', 2) reads binary → 2.
  3. unary. Drops the index — each string parses alone.
  4. Lesson. Never pass multi-arg parsers straight to map.

Example 3 — Cap a Custom Function

Any function that treats a second arg as meaningful can be protected.

Lodash
function tag(name, mark) {
  return mark === undefined ? name : name + String(mark);
}

['hi', 'yo'].map(tag);
// → ['hi0', 'yo1']   // index used as mark!

['hi', 'yo'].map(_.unary(tag));
// → ['hi', 'yo']
Try It Yourself

How It Works

  1. Optional mark. Second param is used when it is not undefined.
  2. map. Passes 0, 1 as that second param.
  3. Bare. Strings pick up numeric marks (hi0, yo1).
  4. unary. Blocks the index — only name is used.

Example 4 — unary vs ary(fn, 1)

Same arity cap — pick the name that reads clearer.

Lodash
var a = _.map(['6', '8', '10'], _.unary(parseInt));
var b = _.map(['6', '8', '10'], _.ary(parseInt, 1));

// a → [6, 8, 10]
// b → [6, 8, 10]
Try It Yourself

How It Works

  1. unary. Always caps at one argument.
  2. ary. Caps at any n — here n = 1.
  3. Same result. Both fix the parseInt map case.
  4. Rule. Prefer unary for readability; ary for other arities.

Example 5 — When You Need the Index

Do not use unary if the callback must see position.

Lodash
var words = ['a', 'b', 'c'];

// Need index — write an explicit lambda
_.map(words, function (word, i) {
  return i + ':' + word;
});
// → ['0:a', '1:b', '2:c']

// unary would hide i — wrong tool here
Try It Yourself

How It Works

  1. Goal. Include the index in the output.
  2. Lambda. Receives (word, i) on purpose.
  3. unary. Would drop i — bad fit.
  4. Rule. Use unary to block extras, not when you need them.

Browser & Runtime Support

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

_.unary()

Available in [email protected] full builds and the lodash/unary modular import. Equivalent to _.ary(fn, 1). A plain arrow (x) => fn(x) is the zero-dependency alternative.

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
_.unary Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import unary from "lodash/unary" for smaller bundles. Reach for unary whenever iterators pass extra args your helper must ignore.

Frequently Asked Questions

_.unary(func) returns a new function that forwards at most one argument to func and ignores any extra arguments.
map calls the iteratee as (value, index, array). parseInt treats the second argument as radix, so indexes 1 and 2 corrupt parses after the first element.
They do the same job. Official docs describe unary as creating a function that accepts up to one argument — equivalent to ary(func, 1) with clearer intent for unary callbacks.
Skip it when your callback needs index or the full collection. Use an explicit (value, index) => … lambda instead.
The wrapper invokes func with the caller's this (unless you bind separately). It mainly strips extra arguments.
Use import unary from "lodash/unary" or require("lodash/unary") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.unary.

Next: _.wrap()

Create a function that provides value to a wrapper as its first argument.

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