Lodash - defaultTo method



Syntax

_.defaultTo(value, defaultValue)

Checks value to determine whether a default value should be returned in its place. The defaultValue is returned if value is NaN, null, or undefined.

Arguments

  • value (*) − The value to check.

  • defaultValue (*) − The default value.

Output

  • (*) − Returns the resolved value.

Example

var _ = require('lodash');
var result = _.defaultTo(5, 6);
 
console.log(result);
 
result = _.defaultTo(undefined, 6); 
console.log(result);

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

Command

\>node tester.js

Output

5
6
lodash_util.htm
Advertisements