Lodash _.constant() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.constant method is used to create a function Creates a function that returns value.

Syntax:

_.constant(value)

Parameters: This method accepts one parameter as mentioned above and described below:

  • value: The value to return from the new function.

Return Value: [Function] It returns the new constant function.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library  
const _ = require("lodash");    
   
// Using _.constant() method
let gfg = _.times(3, _.constant({ 'geeks': 3 }));
   
// Printing the output  
console.log(gfg);

chevron_right


Note: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.

Output:

[Object {geeks: 3}, Object {geeks: 3}, Object {geeks: 3}]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library  
const _ = require("lodash");    
  
// Using _.constant() method
let obj = _.times(2,  _.constant({ 'a': 5 }));
  
// Checking if both objects are same
let gfg = console.log(obj[0] === obj[1] )
  
// Printing the output  
console.log(gfg);

chevron_right


Note: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.

Output:

true

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.