By the end of this tutorial, you’ll use Lodash’s _.nthArg() to build small functions that return one argument from a variadic call.
01
Core Syntax
_.nthArg(n)
02
Zero-based
0 = first arg.
03
Negative n
-1 = last arg.
04
Pass-thru fn
Returns a picker.
05
With _.over
Pick several slots.
06
Out of range
Gets undefined.
Fundamentals
What Is _.nthArg()?
_.nthArg(n) is a function factory. It does not read arguments immediately—it returns a new function that, when invoked with any number of values, passes back only the one at index n.
💡
Beginner tip — two steps
First create the picker: const second = _.nthArg(1). Then call it with your values: second('a', 'b', 'c') → 'b'. The old tutorial named it logNthArgument but the example returns the value—it does not log.
Think of it as (...args) => args[n] packaged as a reusable function—handy when APIs expect a unary callback but your inputs arrive as multiple arguments.
Foundation
📝 Syntax
Create a pass-thru function that selects argument index n (default 0):
javascript
_.nthArg([n = 0])
Syntax Rules
n — zero-based index; negative counts from the end (-1 = last).
Return value — a new function, not the argument itself.
Invoking the result — call with any args; only index n is returned.
Use _.nthArg(-1) for “last argument” without counting arity
Default to _.nthArg(0) when only the first input matters
Pair with _.over to pick multiple slots at once
Name pickers clearly: getSecond, getLast
Handle undefined when indices may be missing
❌ Don’t
Confuse nthArg with _.nth on arrays
Assume 1-based indexing—nthArg(1) is the second arg
Expect errors for out-of-range indices—you get undefined
Overuse where destructuring (a, b) => b is clearer
Forget to invoke the returned picker—it is not the value itself
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.nthArg()
Use these points when picking arguments in functional code.
5
Core concepts
📦01
Factory
Returns picker.
Basics
🔢02
Zero-based
0 = first.
Indexing
🔃03
Negative n
From end.
Advanced
🗃04
undefined
Missing slot.
Safety
🛠05
over
Multi-pick.
Combine
❓ Frequently Asked Questions
_.nthArg(n) returns a new function. When you call that function with any arguments, it returns only the argument at index n—0 for first, 1 for second, and so on.
Yes. _.nthArg(-1) returns the last argument, _.nthArg(-2) the second-to-last—same idea as array indexing from the end.
If you omit n, Lodash uses 0—_.nthArg() is the same as _.nthArg(0) and picks the first argument.
You get undefined. _.nthArg(2)('a', 'b') returns undefined because there is no third argument.
Rest params like (...args) => args[1] are explicit in your function signature. nthArg builds a reusable picker you can pass to _.over, _.flow, or higher-order APIs without writing (...args) each time.
Use it when a library expects a unary function but your data arrives as multiple arguments—pick the one you need. Common in functional pipelines with _.over or method-style invokers.
Did you know?
The official Lodash example uses _.nthArg(-2) on ('a','b','c','d') to get 'c'—the same negative-index rule as _.nth on arrays, but applied to the arguments object of a function call.