Object.assign Function (Object) (JavaScript)

Copies the values from one or more source objects to a target object.

Syntax

Object.assign(target, ...sources );  

Parameters

target
Required. A object to which enumerable properties are copied.

...sources
Required. One or more objects from which enumerable properties are copied.

Exceptions

This function throws a TypeError if there is an assignment error, which terminates the copying operation. A TypeError will be thrown if a target property is not writable.

Remarks

This function returns the target object. Only enumerable own properties are copied from the source object to the target object. You can use this function to merge or clone objects.

null or undefined sources are treated like empty objects and contribute nothing to the target object.

Example

The following code example shows how to merge an object using Object.assign.

var first = { name: "Bob" };  
var last = { lastName: "Smith" };  

var person = Object.assign(first, last);  
console.log(person);  

// Output:  
// { name: "Bob", lastName: "Smith" }   

Example

The following example shows how to clone an object using Object.assign.

var obj = { person: "Bob Smith"};  
var clone = Object.assign({}, obj);  

Requirements

Supported in Microsoft Edge (Edge browser). Also supported in Store apps (Microsoft Edge on Windows 10). See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Not supported in Windows 8.1.