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 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:
javascript
// Requiring the lodash library const _ = require("lodash");     // Using _.constant() methodlet gfg = _.times(3, _.constant({ 'neveropen': 3 }));  // Printing the output console.log(gfg); |
Note: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.
Output:
[Object {neveropen: 3}, Object {neveropen: 3}, Object {neveropen: 3}]
Example 2:
javascript
// Requiring the lodash library const _ = require("lodash");   Â
// Using _.constant() methodlet obj = _.times(2, _.constant({ 'a': 5 }));Â
// Checking if both objects are samelet gfg = console.log(obj[0] === obj[1] )Â
// Printing the output console.log(gfg); |
Note: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.
Output:
true
