Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Underscore.js _.extend() Function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The _.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: This function accept two parameters as mentioned above and described below:

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

Return Value: It returns a copy all of the properties of the source objects over the destination object, and return the destination object.

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <script type="text/javascript" src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
  
        var obj1 = {
            key1: 'Geeks',
        };
  
        var obj2 = {
            key2: 'GeeksforGeeks',
        };
  
        console.log(_.extend(obj1, obj2));
    </script>
</body>
  
</html>


Output:

Example 2:




<!DOCTYPE html>
<html>
  
<head>
    <script type="text/javascript" src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
  
        var obj1 = {
            key1: 'Geeks',
        };
  
        var obj2 = {
            key2: 'GeeksforGeeks',
        };
  
        console.log(_.extend({
            Company: 'GeeksforGeeks',
            Address: 'Noida'
        }, {
            Contact: '+91 9876543210',
            Email: 'abc@gfg.com'
        }, {
            Author: 'Ashok'
        }));
    </script>
</body>
  
</html>


Output:


My Personal Notes arrow_drop_up
Last Updated : 25 Nov, 2021
Like Article
Save Article
Similar Reads
Related Tutorials