Last Updated : 03 Nov, 2023
Lodash _.defaultTo() method is used to check the given value and determine if a default value should be restored in its place. When the value is NaN, null, or undefined, the value given in the defaultValue parameter is returned.
Syntax:_.defaultTo( value, defaultValue );Parameters:
This method returns the resolved value.
Example 1: In this example, we are getting the same value because the given value is a valid number.
JavaScript
// Requiring the lodash library
const _ = require("lodash");
// Return the resolved value
// by _.defaultTo() method
console.log(_.defaultTo(5, 15));
// Return the resolved value
// by _.defaultTo() method
console.log(_.defaultTo(82, 43));
Output:
5 82
Example 2: In this example, we are getting the default value because the given value is not a valid number.
JavaScript
// Requiring the lodash library
const _ = require("lodash");
// When the value is NaN, defaultValue
// is returned by _.defaultTo() method
console.log(_.defaultTo(null, 15));
// When the value is undefined, defaultValue
// is returned by _.defaultTo() method
console.log(_.defaultTo(undefined, 43));
Output:
15 43
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