Friday, October 24, 2025
HomeLanguagesJavascriptLodash _.defaults() Method

Lodash _.defaults() Method

The Lodash _.defaults() Method assign properties of source objects to the destination object for all destination properties that resolve to undefined. Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored. This method mutates the object.

Syntax:

_.defaults( dest_object, [src_obj])

Parameters: This method accepts two parameters as mentioned above and described below:

  • dest_object: This is the destination object.
  • src_obj: These are the source objects.

Return Value: This method returns an object.

Example 1:




// Defining Lodash variable 
const _ = require('lodash'); 
  
a = _.defaults({ 'a': 3 }, { 'b': 1 }, 
    { 'c': 5 }, { 'd': 5 }, { 'e': 5 });
  
console.log(a);


Output:

{ gfg: 3, geek: 1 }

Example 2: 




// Defining Lodash variable 
const _ = require('lodash'); 
  
a = _.defaults({ 'a': 'first setting'}, 
               { 'a': 'second setting but doesn't changes'});
console.log(a);


Output:

{ a: 3, b: 1, c: 5, d: 5, e: 5 }

Example 3: 





Output:

{ a: 'first setting' }

Note: This will not work in normal JavaScript because it requires the lodash library to be installed and can be installed using the following command:

npm install lodash

.

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS