JavaScript Object.assign()
Example
// Create Target Object
const person1 = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
// Create Source Object
const person2 = {firstName: "Anne",lastName: "Smith"};
// Assign Source to Target
Object.assign(person1, person2);
Try it Yourself »
Description
The Object.assign()
method copies properties from
one or more source objects to a target object.
Related Methods:
Object.assign() copies properties from a source object to a target object.
Object.create() creates an object from an existing object.
Object.fromEntries() creates an object from a list of keys/values.
Syntax
Object.assign(target, source(s))
Parameters
Parameter | Description |
target | Required. An existing object. |
source | Required. One or more sources. |
Return Value
Type | Description |
Object | The target object. |
Browser Support
Object.assign()
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |
Object.assign()
is not supported in Internet Explorer.