Lodash _.ary() method

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

What you’ll learn

  • How _.ary(func, n) drops extra parameters before calling func.
  • Why pairing ary with parseInt fixes surprising map output.
  • How default arity and the optional guard argument behave.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Optional reads Function hub and _.after(); know that map passes (value, index) into callbacks.

  • Function arity: how many parameters a function expects (fn.length in simple cases).
  • parseInt(string, radix): second argument is radix—easy to confuse with map indices.

Overview

_.ary(func, n) builds a thin wrapper that invokes func with at most n arguments—extras from callers such as map, event handlers, or timers are ignored. That keeps utilities like parseInt from accidentally consuming metadata arguments.

Iteratee safety

Higher-order APIs often forward three arguments; ary trims them down to what your function truly needs.

Configurable cap

Pick any arity ceiling—not limited to unary helpers.

Tree-shakeable

Import lodash/ary when wrapping is all you need.

Syntax

javascript
_.ary(func, [n = func.length], [guard])
  • func: target function to wrap (must be callable).
  • n: maximum arguments forwarded; omitted or nullish defaults to func.length unless guard resets the branch.
  • guard: when truthy, Lodash ignores the provided n and uses func.length for the cap.
  • Returns: a wrapped function preserving this for the inner call with truncated arguments.
1

parseInt with _.map

Official Lodash example: cap parseInt to one argument so each string parses in base 10 instead of inheriting the numeric index as radix.

javascript
import ary from "lodash/ary";
import map from "lodash/map";

map(["6", "8", "10"], ary(parseInt, 1));
// [6, 8, 10]
Try it Yourself
2

Native map(parseInt) footgun

Contrasting the same input without ary: indices leak into parseInt’s radix slot, producing NaN or unexpected bases.

javascript
["6", "8", "10"].map(parseInt);
// [6, NaN, 2] — index 2 makes radix 2 for "10"
Try it Yourself
3

Cap a multi-argument function

Lodash forwards only the first two operands—extra trailing junk from a loose caller never reaches the reducer.

javascript
import ary from "lodash/ary";

const addAll = (a, b, c) => a + b + (c ?? 0);

const addTwo = ary(addAll, 2);

addTwo(1, 2, 999);
// 3
Try it Yourself

📋 _.ary vs _.unary

HelperArityWhen to reach for it
_.ary(func, n)Any cap you chooseBinary parsers, dual-arg reducers, testing harnesses
_.unary(func)Always 1Shorthand for event handlers and map iteratees needing only the first slot

Both reuse the same internal wrapping machinery; pick unary when brevity beats an explicit number.

Pitfalls to avoid

Arity

Too small n

Dropping arguments you still need yields silent logic bugs—verify against real caller signatures.

Defaults

func.length quirks

Rest parameters and native builtins may report 0 or misleading lengths; set n explicitly when unsure.

Radix

Still pass radix?

Even with ary(parseInt, 1), specify 10 when parsing arbitrary user strings—this helper only fixes the map arity issue.

❓ FAQ

Array.prototype.map invokes parseInt(value, index). From the second item onward the index becomes the radix—illegal or surprising values yield NaN or odd parses. _.ary(parseInt, 1) forwards only the string.
Lodash substitutes func.length when n is null or undefined after accounting for the optional guard argument.
When truthy, Lodash ignores the supplied n value and falls back to func.length—mainly so wrappers compose cleanly when Lodash passes extra trailing arguments (iteratee contexts).
unary hard-codes a single argument. ary lets you cap at any n; unary(fn) matches ary(fn, 1).
Use import ary from "lodash/ary" for tree-shakeable bundles.

Summary

Did you know?

Lodash documents _.unary(func) as arity-1 sugar—in practice it is implemented as _.ary(func, 1), so anything unary does you can express explicitly with ary.

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