A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-constant-method/ below:

Lodash _.constant() Method - GeeksforGeeks

Lodash _.constant() Method

Last Updated : 20 Apr, 2022

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:

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

Example 1:

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

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:

javascript
// 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);

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

Output:

true


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4