Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Setting Default Values:
4.2.
JavaScript
4.3.
Multiple Source Objects:
4.4.
JavaScript
4.5.
Initializing Settings:
4.6.
JavaScript
4.7.
Defaults in Function Parameters:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.defaults() differ from _.assign()?
5.2.
Does _.defaults() perform a deep merge?
5.3.
Is _.defaults() suitable for setting up complex configurations?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.defaults() Method

Author Riya Singh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In programming, especially in configurations and settings management, there's often a need to ensure that objects have certain default values. Lodash's _.defaults() method is designed to address this requirement. It assigns default values to properties in an object, filling in undefined properties in the object with the first value it finds in a series of source objects.

Lodash _.defaults() Method

This method is particularly useful in scenarios where you need to set up default settings or configurations.

Why This Function is Used

The _.defaults() function is used to fill in undefined properties in an object with values from subsequent source objects. It's a convenient method to ensure that an object has a set of default values, especially useful in settings where an object needs to be initialized with a predefined set of properties, but those properties can be overridden by user inputs or other sources.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

JavaScript

var _ = require('lodash');

var config = { 'width': 100 };

var defaultConfig = { 'width': 200, 'height': 300 };

_.defaults(config, defaultConfig);

console.log(config);

 Output: 

{ 'width': 100, 'height': 300 }


Demonstrates setting default values in a configuration object.

Multiple Source Objects:

  • JavaScript

JavaScript

var object = {};

var defaults = { 'a': 1, 'b': 2 };

var moreDefaults = { 'b': 3, 'c': 3 };

_.defaults(object, defaults, moreDefaults);

console.log(object);

Output:

 { 'a': 1, 'b': 2, 'c': 3 }


Shows how _.defaults() assigns values from multiple source objects.

Initializing Settings:

  • JavaScript

JavaScript

var userSettings = { 'theme': 'dark' };

var systemDefaults = { 'theme': 'light', 'fontSize': 'medium' };

_.defaults(userSettings, systemDefaults);

console.log(userSettings);

Output:

 { 'theme': 'dark', 'fontSize': 'medium' }


An example of initializing user settings with default values.

Defaults in Function Parameters:

  • JavaScript

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);

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Lodash _.defaultTo() Method
Next article
Lodash _.functionsIn() Method
Live masterclass