Syntax, Parameter and Return Value
Syntax:
_.rest(func, [start=func.length-1])
Parameters:
-
func (Function): The function to apply the rest parameter to.
- [start=func.length-1] (number): The position at which to start collecting arguments.
Return Value:
(Function) - Returns the new function.
Examples
Collecting Additional Arguments:
JavaScript
var _ = require('lodash');
function assembleMessage(firstWord, ...restWords) {
return firstWord + ' ' + restWords.join(' ');
}
var enhancedAssembleMessage = _.rest(assembleMessage);
console.log(enhancedAssembleMessage('Hello', 'world', 'from', 'Lodash!'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Hello world from Lodash!'
Demonstrates collecting all arguments beyond the first into an array.
Creating Variadic Functions:
JavaScript
function sum(...numbers) {
return numbers.reduce((total, n) => total + n, 0);
}
var variadicSum = _.rest(sum, 2);
console.log(variadicSum(1, 2, 3, 4));

You can also try this code with Online Javascript Compiler
Run Code
Output:
9 (sum of 2, 3, 4)
Shows creating a function that sums all arguments after the first two.
Handling Event Listeners:
JavaScript
function handleEvent(eventType, ...details) {
console.log(`Event: ${eventType}, Details:`, details);
}
var enhancedHandleEvent = _.rest(handleEvent);
enhancedHandleEvent('click', 'button1', 'header', 'footer');function handleEvent(eventType, ...details) {
console.log(`Event: ${eventType}, Details:`, details);
}
var enhancedHandleEvent = _.rest(handleEvent);
enhancedHandleEvent('click', 'button1', 'header', 'footer');

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Event: click, Details: ['button1', 'header', 'footer']'
An example of using _.rest() to handle variadic arguments in event listeners.
Processing Command-Line Arguments:
JavaScript
function processCommands(command, ...options) {
console.log(`Command: ${command}, Options:`, options);
}
var processCommandsVariadic = _.rest(processCommands);
processCommandsVariadic('start', '--verbose', '--timeout=20');

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Command: start, Options: ['--verbose', '--timeout=20']'
Demonstrates processing a list of command-line arguments.
Frequently Asked Questions
How is _.rest() different from native JavaScript rest parameters?
_.rest() is similar to native JavaScript rest parameters but offers more control, allowing you to specify from which argument to start collecting the rest of the arguments.
Can _.rest() be used with functions of any arity?
Yes, _.rest() can be used with functions of any arity. It's particularly effective with functions where the number of arguments isn't fixed.
Is the original function modified by _.rest()?
No, _.rest() creates a new function and does not modify the original function. The original function remains as it is.
Conclusion
Lodash's _.rest() method offers a convenient way to handle variadic functions, allowing for the easy grouping of arguments beyond a certain point. It's a useful tool for functions that need to process an indefinite number of arguments or need to operate on the latter part of their arguments.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.