Thursday, December 18, 2025
HomeLanguagesJavascriptUnderscore.js _.fix() Method

Underscore.js _.fix() Method

The Underscore.js _.fix() method fixes the arguments to a function based on the parameter template defined by the presence of values and the _ placeholder.

The function takes a parameter that is replaced by _ in given values.

Syntax:

_.fix( fun, [values] )

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

  • fun: This parameter holds the given function.
  • values: The values passed to the fun.

Return Value: It 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: 

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function mul(a, b, c, d){
    return a*b*c*d;
}
  
// Making curried function
var gfgFunc = _.fix(mul, 1,2,_,4);
  
// 3 is replaced by _ in given values
console.log("Multiplication is :",gfgFunc(3));


Output:

Multiplication is : 24

Example 2:

Javascript




// Defining Underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function add(a, b, c){
    return a+b+c;
}
  
// Making fixed function
var gfgFunc = _.fix(add, 1, 2, _);
  
console.log("Addition is :",gfgFunc(3));


Output: 

Addition is : 6

Example 3:

Javascript




// Defining Underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function fun(str){
    return str;
}
  
// Making fixed function
var gfgFunc = _.fix(fun, _);
  
console.log("Coding Platform :",
      gfgFunc("neveropen"));


Output: 

Coding 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
32455 POSTS0 COMMENTS
Milvus
108 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11957 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12035 POSTS0 COMMENTS
Shaida Kate Naidoo
6957 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6910 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS