How does Object.assign() handle properties with the same key?

Object.assign() overwrites properties with the same key from later sources.

When using Object.assign(), if multiple source objects have properties with the same key, the property from the last source object in the parameter list will overwrite the previous ones. This means that the order of the source objects is important, as later sources can override properties from earlier ones.

const o1 = { a: 1, b: 1 };
const o2 = { b: 2, c: 2 };
const result = Object.assign({}, o1, o2);
console.log(result); // { a: 1, b: 2, c: 2 }
More cards4
Show all