Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Accessing a Specific Argument:
4.2.
JavaScript
4.3.
Using with Higher-Order Functions:
4.4.
JavaScript
4.5.
Accessing the Last Argument:
4.6.
JavaScript
4.7.
Combining with Other Lodash Functions:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.nthArg() handle out-of-bounds indices?
5.2.
Can _.nthArg() be used with functions that have a variable number of arguments?
5.3.
Is _.nthArg() commonly used in real-world applications?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.nthArg() Method

Author Riya Singh
0 upvote

Introduction

In JavaScript, especially when working with functions that take multiple arguments, there may be situations where you need to create a function that returns the nth argument passed to it. Lodash's _.nthArg() method is a utility function designed for this purpose. It creates a function that gets the nth argument passed to the calling function. 

 Lodash _.nthArg() Method

This method is particularly useful in functional programming paradigms and when working with higher-order functions.

Why This Function is Used

The _.nthArg() function is used to create a simple and reusable way to access a specific argument from a list of arguments in a function. This is essential in scenarios where you need to manipulate or access one particular argument frequently or in a standardized manner, enhancing code readability and reducing redundancy.

Syntax, Parameter and Return Value

Syntax: 

_.nthArg([n=0])

Parameters:

[n=0]: The index of the argument to return, with negative indexing from the end (e.g., -1 for the last argument).

Return Value: 

(Function) - Returns the new pass-thru function.

Examples 

Accessing a Specific Argument:

  • JavaScript

JavaScript

var _ = require('lodash');

var getSecondArg = _.nthArg(1);

console.log(getSecondArg('a', 'b', 'c'));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 'b'


Demonstrates creating a function that returns the second argument passed to it.

Using with Higher-Order Functions:

  • JavaScript

JavaScript

var numbers = ['one', 'two', 'three'];

var getThird = _.nthArg(2);

var thirdItems = numbers.map(getThird);

console.log(thirdItems);
You can also try this code with Online Javascript Compiler
Run Code

 Output:

 [undefined, undefined, 'three']


Shows using _.nthArg() in a map function to get the third argument (index) of each element.

Accessing the Last Argument:

  • JavaScript

JavaScript

var getLastArg = _.nthArg(-1);

console.log(getLastArg(1, 2, 3, 4, 5));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 5


An example of using _.nthArg() to access the last argument passed to a function.

Combining with Other Lodash Functions:

  • JavaScript

JavaScript

var _ = require('lodash');

var complexFunction = _.flow([_.add, _.nthArg(-1), _.multiply(2)]);

console.log(complexFunction(5, 3));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 6 ((5 + 3), then 3, then 3 * 2)

Demonstrates combining _.nthArg() with other Lodash functions to create a more complex operation.

Frequently Asked Questions

How does _.nthArg() handle out-of-bounds indices?

If the specified index is out of bounds (i.e., the function doesn't receive enough arguments), _.nthArg() will return undefined.

Can _.nthArg() be used with functions that have a variable number of arguments?

Yes, _.nthArg() works well with variadic functions, allowing you to extract a specific argument regardless of the total number of arguments passed.

Is _.nthArg() commonly used in real-world applications?

_.nthArg() is particularly useful in functional programming patterns and situations where operations on specific arguments are required repeatedly across different functions.

Conclusion

Lodash's _.nthArg() method offers a concise and effective way to create functions that return a specific argument from the list of arguments passed. It's a valuable tool in scenarios that require manipulation or retrieval of specific arguments, enhancing the flexibility and readability of functional programming in JavaScript.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass