ES6 – Object.assign()

Hi, in this post we are going to learn about this new method .assign() that is of the Object.

Object.assign() is used to copy the values of all enumerable own properties from one or more source objects to a target object, this method will return us the targetObj.

const firstObject = { a: 1 };
const secondObject = { b: 2 };
const thirdObject = { c: 3, d: {} };

const resultObject = Object.assign(firstObject, secondObject, thirdObject);

console.log("resultObject --->", resultObject);


Note: It doesn’t copy the [[prototype]] property of sources, it ignores copying keys with null and undefined values.

By Cristina Rojas.