Syntax, Parameter and Return Value
Syntax:
_.defaultTo(value, defaultValue)
Parameters:
-
value: The value to check for validity.
- defaultValue: The default value to return if the original value is invalid.
Return Value:
Returns the resolved value (either the original value if valid, or the default value).
Examples
Setting Default Value for Potentially Undefined Variable:
JavaScript
var _ = require('lodash');
var possiblyUndefined = undefined;
var safeValue = _.defaultTo(possiblyUndefined, 10);
console.log(safeValue);

You can also try this code with Online Javascript Compiler
Run Code
Output:
10
Demonstrates using _.defaultTo() to set a default value when the original variable is undefined.
Handling Null Values in Data Processing:
JavaScript
var userAge = null;
var ageForProcessing = _.defaultTo(userAge, 18);
console.log(ageForProcessing);

You can also try this code with Online Javascript Compiler
Run Code
Output:
18
Shows setting a default value for a variable that might be null.
Preventing NaN in Mathematical Operations:
JavaScript
function calculateDiscount(price, discount) {
discount = _.defaultTo(discount, 0);
return price - (price * discount);
}
console.log(calculateDiscount(100, NaN));

You can also try this code with Online Javascript Compiler
Run Code
Output:
100
An example of using _.defaultTo() to avoid NaN results in calculations.
Using with Optional Object Properties:
JavaScript
var config = { timeout: null };
var timeoutValue = _.defaultTo(config.timeout, 1000);
console.log(timeoutValue);

You can also try this code with Online Javascript Compiler
Run Code
Output:
1000
Demonstrates setting a default value for an optional property in an object.
Frequently Asked Questions
What types of values are considered invalid by _.defaultTo()?
_.defaultTo() considers NaN, null, and undefined as invalid values. Other falsy values like 0, '', and false are considered valid.
How does _.defaultTo() differ from logical OR (||) operator?
The logical OR operator (||) treats all falsy values (0, '', false, null, undefined, NaN) as invalid, whereas _.defaultTo() specifically checks for null, undefined, and NaN.
Is _.defaultTo() commonly used in real-world applications?
_.defaultTo() is particularly useful in applications where robustness and error handling are important, especially in dealing with external data or user inputs.
Conclusion
Lodash's _.defaultTo() method is an effective tool for ensuring that variables have valid values, providing a simple way to set default values in case of null, undefined, or NaN. It enhances code reliability by preventing errors related to invalid data.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.