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

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

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'a': { 'b': 2, 'c': 3 }, 'd': 4 }
Shows how _.defaultsDeep() assigns values from multiple source objects to a nested object.
Initializing Deeply Nested Settings:
JavaScript
var userSettings = { 'preferences': { 'theme': 'dark' } };
var systemDefaults = { 'preferences': { 'theme': 'light', 'fontSize': 'medium' }, 'notifications': true };
_.defaultsDeep(userSettings, systemDefaults);
console.log(userSettings);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'preferences': { 'theme': 'dark', 'fontSize': 'medium' }, 'notifications': true }
An example of initializing deeply nested user settings with default values.
Defaults in Complex Data Structures:
JavaScript
var project = {
name: 'My Project',
config: {
version: '1.0',
build: { optimized: false }
}
};
var defaultProjectConfig = {
config: {
build: { optimized: true, debug: true },
environment: 'development'
}
};
_.defaultsDeep(project, defaultProjectConfig);
console.log(project);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ name: 'My Project', config: { version: '1.0', build: { optimized: false, debug: true }, environment: 'development' } }
Demonstrates using _.defaultsDeep() for complex data structures in project configuration.
Frequently Asked Questions
How does _.defaultsDeep() differ from _.defaults()?
While _.defaults() only fills in undefined top-level properties, _.defaultsDeep() does so recursively, affecting nested objects as well.
Does _.defaultsDeep() perform a deep clone of source properties?
_.defaultsDeep() does a shallow copy of each source property. For properties that are objects, it creates a new object, but if the property is another type, like an array, it simply copies the reference.
Can _.defaultsDeep() handle arrays in objects?
For arrays within objects, _.defaultsDeep() assigns them as they are, without merging their contents. If a default array is expected to merge with an array in the object, a custom implementation is required.
Conclusion
Lodash's _.defaultsDeep() method is an effective tool for initializing objects with default values, including deeply nested properties. It's particularly valuable in complex settings where configurations or data structures span multiple levels and require comprehensive defaulting.
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.