freeCodeCamp/guide/english/javascript/standard-objects/object/object-assign/index.md

2.8 KiB

title
Object Assign

Object Assign

This is a stub. Help our community expand it.

This quick style guide will help ensure your pull request gets accepted.

The Object.assign() method is used to 1) add properties and values to an existing object, 2) make a new copy of an existing object, or 3) combine multiple existing objects into a single object. The Object.assign() method requires one targetObject as a parameter and can accept an unlimited number of sourceObjects as additional parameters.

Important to note here is that the targetObject parameter will always be modified. If that parameter points to an existing object, then that object will be both modified and copied. If, however, you wish to create a copy of an object without modifying that original object, you can pass an empty object {} as the first (or targetObject) parameter and the object to be copied as the second (or sourceObject) parameter.

If objects passed as parameters into Object.assign() share the same properties (or keys), property values that come later in the parameters list will overwrite those which came earlier.

Syntax

Object.assign(targetObject, ...sourceObject)

Return Value

Object.assign() returns the targetObject.

Examples

Modifying and Copying targetObject

let obj = {name: 'Dave', age: 30};

let objCopy = Object.assign(obj, {coder: true});

console.log(obj); // returns { name: 'Dave', age: 30, coder: true }
console.log(objCopy); // returns { name: 'Dave', age: 30, coder: true }

Copying targetObject Without Modification

let obj = {name: 'Dave', age: 30};

let objCopy = Object.assign({}, obj, {coder: true});

console.log(obj); // returns { name: 'Dave', age: 30 }
console.log(objCopy); // returns { name: 'Dave', age: 30, coder: true }

Objects With Same Properties

let obj = {name: 'Dave', age: 30, favoriteColor: 'blue'};

let objCopy = Object.assign({}, obj, {coder: true, favoriteColor: 'red'});

console.log(obj); // returns { name: 'Dave', age: 30, favoriteColor: 'blue' }
console.log(objCopy); // { name: 'Dave', age: 30, favoriteColor: 'red', coder: true }

More Information:

MDN
Intro to Object.assign in ES6 (Video)