Underscore.js _.extend() Function

Underscore.js _.extend() function is used to create a copy of all of the properties of the source objects over the destination object and return the destination object. The nested arrays or objects will be copied by using reference, not duplicated.

Syntax:

_.extend(destination, *sources);

Parameters:

  • destination: This parameter holds the destination object file.
  • sources: This parameter holds the source object file.

Return Value:

It returns a copy of all of the properties of the source objects over the destination object and returns the destination object.

Example 1: In this example, we are using the Underscore.js _.extend() function.

html




<!DOCTYPE html>
<html>
 
<head>
    <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
    </script>
</head>
 
<body>
    <script type="text/javascript">
 
        let obj1 = {
            key1: 'Beginner',
        };
 
        let obj2 = {
            key2: 'w3wiki',
        };
 
        console.log(_.extend(obj1, obj2));
    </script>
</body>
 
</html>


Output:

Example 2: In this example, we are using the Underscore.js _.extend() function.

html




<!DOCTYPE html>
<html>
 
<head>
    <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
    </script>
</head>
 
<body>
    <script type="text/javascript">
 
        let obj1 = {
            key1: 'Beginner',
        };
 
        let obj2 = {
            key2: 'w3wiki',
        };
 
        console.log(_.extend({
            Company: 'w3wiki',
            Address: 'Noida'
        }, {
            Contact: '+91 9876543210',
            Email: 'abc@gfg.com'
        }, {
            Author: 'Ashok'
        }));
    </script>
</body>
 
</html>


Output:



Contact Us