Friday, January 30, 2026
HomeLanguagesJavascriptUnderscore.js _.curry() Method

Underscore.js _.curry() Method

The Underscore.js _.curry() method returns a curried version of the given function. If the reverse is true, arguments will be processed from right to left.

Syntax:

_.curry( fun, reverse )

Parameters: This method takes two parameters as listed above and discussed below.

  • fun: This is the given function.
  • reverse: It is an optional parameter and determines whether the arguments will be reversed or not

Return Value: It returns a curried version of the 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: 

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function fun(a, b, c, d){
    return a + b + c + d;
}
  
// Making curried function
var gfgFunc = _.curry(fun);
  
// Only operates for arguments same
// as the number in function
console.log("Addition is :",
        gfgFunc(2)(23)(2)(3));
  
// Not adds for less arguments
console.log(gfgFunc(1)(2));


Output:

Addition is : 30
[Function: mustBeUnary]

Example 2:

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function fun(a, b, c) {
    return a * b * c;
}
  
// Making curried function
var gfgFunc = _.curry(fun);
  
// Only operates for arguments same
// as the number in function
console.log("Multiplication is :",
        gfgFunc(2)(23)(2));
  
// Not multiplies for less arguments
console.log(gfgFunc(1)(2));


Output: 

Multiplication is : 92
[Function: mustBeUnary]

Example 3: Arguments are processed right to left for reversed = true.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function fun(a, b, c){
    return arguments;
}
  
// Making curried function
var gfgFunc = _.curry(fun,true);
  
// Only operates for arguments same
// as the number in function
console.log("curried arguments are :",
    gfgFunc("arg1")("arg2")("arg3"));


Output: Arguments are stored in reverse order.

curried arguments are : [Arguments] 
    { '0': 'arg3', '1': 'arg2', '2': 'arg1' }

Example 4: Arguments are processed left to right for reversed = false.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function fun(a, b, c){
    return arguments;
}
  
// Making curried function
var gfgFunc = _.curry(fun,false);
  
// Only operates for arguments same
// as the number in function
console.log("curried arguments are :",
    gfgFunc("arg1")("arg2")("arg3"));


Output: 

curried arguments are : [Arguments] 
    { '0': 'arg1', '1': 'arg2', '2': 'arg3' }
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

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS