Lodash - constant method


Advertisements

Syntax

_.constant(value)

Creates a function that returns value.

Arguments

  • value (*) − The value to return from the new function.

Output

  • (Function) − Returns the new constant function.

Example

var _ = require('lodash');
var check = _.cond([
   [_.matches({ 'a': 1 }), _.constant('matches A')],
   [_.conforms({ 'b': _.isNumber }), _.constant('matches B')],
   [_.stubTrue, _.constant('no match')]
]);
 
console.log(check({ 'a': 1, 'b': 2 }));
console.log(check({ 'a': 0, 'b': 1 }));
console.log(check({ 'a': '1', 'b': '2' }));

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

matches A
matches B
no match
lodash_util.htm
Advertisements