Lodash _.nthArg() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Util utilities

What You’ll Learn

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.

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.

📝 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.
  • Missing index — returns undefined (no throw).
  • Default_.nthArg() equals _.nthArg(0) (first argument).
javascript
import nthArg from "lodash/nthArg";



const second = nthArg(1);



second("apple", "banana", "orange");

// "banana"

⚡ Quick Reference

TaskCode patternNotes
First argument_.nthArg(0)Same as default
Second argument_.nthArg(1)Index 1
Last argument_.nthArg(-1)From end
Dynamic index(i) => _.nthArg(i)Factory
Pick two slots_.over([_.nthArg(0), _.nthArg(1)])With over
Missing arg_.nthArg(2)('a','b')→ undefined
Returns
Function

Argument picker

Index
Number

0 or negative

Pair with
over

Multi-pick

Category
Util

Function

🧰 Parameters

Argument to _.nthArg() and behavior of the returned picker:

n Optional

Zero-based index of the argument to return. Defaults to 0 when omitted.

_.nthArg(1)
n < 0 From end

-1 selects the last argument, -2 the second-to-last—like array slice indexing.

_.nthArg(-1)
returned fn Picker

Variadic function: picker(a, b, c, ...) returns the value at index n.

second("a", "b")
out of range undefined

If fewer arguments were passed than the index requires, result is undefined.

_.nthArg(5)("only")

For array elements by index use _.nth from the Array category—nthArg works on function call arguments, not array properties.

Examples Gallery

Practical _.nthArg() patterns with copy-ready code, sample output, and interactive Try It Yourself labs.

📚 Getting Started

Pick the second argument and understand zero-based indexing.

Example 1 — Get the second argument

Official-style example: index 1 is the second value.

javascript
const getSecond = _.nthArg(1);



console.log(getSecond("apple", "banana", "orange"));

// "banana"
Try It Yourself

How It Works

_.nthArg(1) closes over index 1. The returned function ignores arity and simply returns arguments[1] (conceptually).

Example 2 — First argument (default index)

_.nthArg(0) or plain _.nthArg() picks the first value.

javascript
const getFirst = _.nthArg(0);



console.log(getFirst("apple", "banana", "orange"));

// "apple"



console.log(_.nthArg()("x", "y"));

// "x" — default n is 0

How It Works

Equivalent to _.identity() when exactly one argument matters—but nthArg still accepts extra args and always returns index 0.

📈 Practical Patterns

Negative indices, safety, and dynamic pickers.

Example 3 — Last argument with negative index

Lodash docs pattern: _.nthArg(-1) and _.nthArg(-2).

javascript
const getLast = _.nthArg(-1);

const getSecondLast = _.nthArg(-2);



console.log(getLast("a", "b", "c", "d"));

console.log(getSecondLast("a", "b", "c", "d"));

// "d"

// "c"
Try It Yourself

How It Works

Negative n counts from the end of the arguments list—ideal for “last wins” APIs without knowing arity upfront.

Example 4 — Out-of-range index

Requesting a missing argument returns undefined—no error thrown.

javascript
const getThird = _.nthArg(2);



console.log(getThird("apple", "banana"));

// undefined



console.log(getThird("apple", "banana", "orange"));

// "orange"

How It Works

Guard or default downstream if the picked value may be missing—especially with user-controlled argument lists.

Example 5 — Dynamic index factory

Build pickers at runtime based on configuration.

javascript
const createPicker = (position) => {

  if (position === "first") return _.nthArg(0);

  if (position === "last") return _.nthArg(-1);

  return _.nthArg(1);

};



const pick = createPicker("last");



console.log(pick("apple", "banana", "orange"));

// "orange"

How It Works

Each call to _.nthArg(i) produces a independent picker—store or return it from factories and routers.

🚀 Beyond the Basics

Combine with _.over to read multiple slots at once.

Example 6 — Pick first and second with _.over

Run several nthArg pickers on the same argument list.

javascript
const pickFirstAndSecond = _.over([_.nthArg(0), _.nthArg(1)]);



console.log(pickFirstAndSecond("apple", "banana", "orange"));

// ["apple", "banana"]
Try It Yourself

How It Works

_.over applies each iteratee to the same inputs and collects results—nthArg slots fit naturally as lightweight extractors.

🧠 How _.nthArg() Works

1

Capture index n

Lodash stores the requested index (including negative offsets).

Setup
2

Return picker fn

A variadic function is created—ready to plug into higher-order APIs.

Factory
3

Invoke with args

When called, the picker reads the argument at index n from the invocation.

Execute
=

Selected argument

That single value—or undefined if the slot is empty.

📝 Notes

  • Indexing starts at 0_.nthArg(1) is the second argument, not the first.
  • Negative n counts from the end—-1 is the last argument passed.
  • _.nthArg() returns a function—call that function with your values to get the pick.
  • Do not confuse with _.nth(array, n), which indexes into arrays.
  • Combine with _.over() to extract multiple arguments in one call.
  • Next in the series: _.over()—apply several iteratees to the same inputs.

Conclusion

_.nthArg(n) turns “give me argument number n” into a reusable function—clean for variadic calls, negative “last arg” picks, and pipelines with _.over.

Remember zero-based indexing, handle undefined when args are missing, and reach for rest parameters when a plain inline function is clearer.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about _.nthArg()

Use these points when picking arguments in functional code.

5
Core concepts
🔢 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.

Practice _.nthArg() in the Live Editor

Pick the second argument, try negative indices, and combine pickers with _.over.

Open Try It editor →

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