Open In App

Underscore.js _.restArguments() Function

Last Updated : 25 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc. even without using any built-in objects.

The _.restArguments() function is an inbuilt function in Underscore.js library of JavaScript which is used to find a version of the function which when invoked can receive all the arguments from and beyond the stated startIndex that is collected into a single array. The number of arguments to the function would be used to determine the startIndex when a definite value is not given.

Syntax:

_.restArguments( function, startIndex )

Parameters: This method accepts two parameters as mentioned above and described below:

  • function: It is the stated function.
  • startIndex: It is the start position of the rest parameter. It is an optional parameter.

Return Value: This method returns a version of the function which is when invoked can receive all the arguments from and beyond the stated index.

Example 1: In this example, a user-defined function is used.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
        // Calling restArguments method
        // with its parameter
        var writes =
            _.restArguments(function (authors, portal) {
                return authors + portal;
            });
  
        // Calling write with its values 
        console.log(
            writes(
                ['Nidhi1352', ' GeeksforGeeks!']
            )
        );
    </script>
</body>
  
</html>


Output:

Nidhi1352, GeeksforGeeks!

Example 2: In this example, a starting index is passed along with the user-defined function.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
        // Calling restArguments method
        // with its parameter
        var writes =
            _.restArguments(
                function (authors, portal, articles) {
                    return authors + portal + articles;
                }, [2]);
  
        // Calling writes with its values 
        console.log(
            writes(
                ['Nidhi1352', ' GeeksforGeeks!', ' 700 ']
            )
        );
    </script>
</body>
  
</html>


Output:

Nidhi1352, GeeksforGeeks!, 700 undefined

Example 3:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
        var call =
            _.restArguments(function (who, whom) {
                return who + ' ' +
                    _.initial(whom).join(', ') +
                    (_.size(whom) > 2 ? ', & ' : '') +
                    _.last(whom);
            });
  
        // Calling the function above
        // with its values
        console.log(
            call(
                'She called', 'me', 'her', 'you.'
            )
        );
    </script>
</body>


Output:

She called me, her, & you.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads