HomeLanguagesJavascriptUnderscore.js _.quaternary() Method

Underscore.js _.quaternary() Method

The Underscore.js _.quaternary() returns a new function that accepts only four arguments and passes these arguments to the given function. Additional arguments are discarded.

Syntax:

_.quaternary( fun )

Parameters: This method takes a single parameter as listed above and discussed below:

  • fun: This is the given function passed as parameter.

Return Value: This method returns a new function.

Note: To execute the below examples, you have to install the underscore-contrib library by using this command prompt and execute the following command.

npm install underscore-contrib

Example 1: 




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function fun() {
    return arguments;
}
  
// Making quaternary function
var gfgFunc = _.quaternary(fun);
  
console.log("Arguments are :", gfgFunc(1, 2, 3, 4));


Output:

Arguments are : [Arguments] { '0': 1, '1': 2, '2': 3, '3': 4 }

Example 2: In this example, we will add arguments but only the first 4 arguments will be using this method.




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function add() {
    s = 0
    for(i = 0; i < 4; i++) {
        s += arguments[i]
    }
  
    return s;
}
  
// Making quaternary function
var gfgFunc = _.quaternary(add);
  
// Rest arguments are excluded
console.log("Sum of first 4 arguments is :", 
gfgFunc(1, 2, 3, 4, 5, 6, 7));


Output: 

Sum of first 4 arguments is : 10
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32559 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6935 POSTS0 COMMENTS
Nicole Veronica
12056 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12174 POSTS0 COMMENTS
Shaida Kate Naidoo
7072 POSTS0 COMMENTS
Ted Musemwa
7316 POSTS0 COMMENTS
Thapelo Manthata
7028 POSTS0 COMMENTS
Umr Jansen
7019 POSTS0 COMMENTS