Open In App

Underscore.js _.defaults() Function

Underscore.js _.defaults() function returns the object after filling in its undefined properties with the first value present in the following list of default objects.

Syntax:

_.defaults(object, *defaults);

Parameters:

Return Value:

It returns the object after filling in its undefined properties with the first value present in the following list of default objects.



Example 1: The below code example implements the _.defaults() function in underscore.js.




<!DOCTYPE html>
<html>
 
<head>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const info = {
            Company: 'GeeksforGeeks',
            Address: 'Noida',
            Contact: '+91 9876543210'
        };
 
        console.log(_.defaults(info,
            {
                Contact: '+91 9898989898',
                Name: 'Rakesh'
            })
        );
    </script>
</body>
 
</html>

Output:



Example 2: This is another implementation of the _.defaults() function.




<!DOCTYPE html>
<html>
 
<head>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
 
        const info = {
            Company: 'GeeksforGeeks',
            Address: 'Noida',
            Contact: '+91 9876543210'
        };
 
        const def = {
            Name: 'Ashok',
            Age: '34',
            Company: 'GFG'
        }
 
        console.log(_.defaults(info, def));
    </script>
</body>
 
</html>

Output:


Article Tags :