Skip to main content

JavaScript - Object.assign()


Object.assign#

Javascript Object assign() is a built-in function that is used to copy the values of all it's own enumerable properties from one or more source objects to the target object.
Object.assign() method will return a target object.
Javascript Object.assign() method is used for cloning an object.
It is used to merge objects with the same properties.

const user = {
name: "Kim",
active: true,
cart: [],
purchases: [],
};
// We start with an empty object, we clone the user object
// and also add a new property to the object.
const cloneMergeObject = Object.assign({}, user, { newprop: "haha" });
console.log(cloneMergeObject);
/* { name: 'Kim',
active: true,
cart: [],
purchases: [],
newprop: 'haha' } */
Last updated on