Friday, November 21, 2025
HomeLanguagesJavascriptUnderscore.js _.unary() Method

Underscore.js _.unary() Method

The Underscore.js _.unary() method returns a new function that accepts only one argument and passes this argument to the given function. Additional arguments are discarded.

Syntax:

_.unary( 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 unary function
var gfgFunc = _.unary(fun);
  
console.log("Arguments are :", 
    gfgFunc(1, 2, 3));


Output:

Arguments are : [Arguments] { '0': 1 }

Example 2:




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


Output: 

Arguments are : [Arguments] { '0': 1 }

Example 3:




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function fun(a){
    return a;
}
  
// Making unary function
var gfgFunc = _.unary(fun);
  
// Only one argument is passed
console.log("Best Platform :", 
    gfgFunc("neveropen"));


Output: 

Best Platform : neveropen
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
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11995 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7166 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS