Thursday, February 26, 2026
HomeLanguagesJavascriptLodash _.constant() Method

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 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() method
let 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() 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
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
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS