Syntax, Parameter and Return Value
Syntax:
_.defaults(object, [sources])
Parameters:
-
object (Object): The destination object.
- [sources] (...Object): The source objects.
Return Value:
(Object) - Returns the modified object.
Examples
Setting Default Values:
JavaScript
var _ = require('lodash');
var config = { 'width': 100 };
var defaultConfig = { 'width': 200, 'height': 300 };
_.defaults(config, defaultConfig);
console.log(config);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'width': 100, 'height': 300 }
Demonstrates setting default values in a configuration object.
Multiple Source Objects:
JavaScript
var object = {};
var defaults = { 'a': 1, 'b': 2 };
var moreDefaults = { 'b': 3, 'c': 3 };
_.defaults(object, defaults, moreDefaults);
console.log(object);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'a': 1, 'b': 2, 'c': 3 }
Shows how _.defaults() assigns values from multiple source objects.
Initializing Settings:
JavaScript
var userSettings = { 'theme': 'dark' };
var systemDefaults = { 'theme': 'light', 'fontSize': 'medium' };
_.defaults(userSettings, systemDefaults);
console.log(userSettings);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'theme': 'dark', 'fontSize': 'medium' }
An example of initializing user settings with default values.
Defaults in Function Parameters:
JavaScript
function createWidget(options) {
var defaultOptions = { 'color': 'blue', 'size': 'large' };
_.defaults(options, defaultOptions)
// Create widget with options
}
var widgetOptions = { 'color': 'green' };
createWidget(widgetOptions);
console.log(widgetOptions);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'color': 'green', 'size': 'large' }
Demonstrates using _.defaults() to set default parameters for a function.
Frequently Asked Questions
How does _.defaults() differ from _.assign()?
While _.assign() overwrites properties in the target object with properties from the sources, _.defaults() only assigns undefined properties in the target object from the sources.
Does _.defaults() perform a deep merge?
No, _.defaults() performs a shallow merge. It only assigns undefined properties at the top level of the target object.
Is _.defaults() suitable for setting up complex configurations?
_.defaults() is suitable for simple to moderately complex configuration setups. For deep merging of default configurations, you might need a more sophisticated approach, like _.mergeWith() or custom logic.
Conclusion
Lodash's _.defaults() method offers a convenient way to ensure that objects have specified default values. It's particularly useful in initializing configurations and settings, ensuring objects are properly set up without overwriting existing values.
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.