- Courses
- JavaScript
- Objects
- Object.assign
- Return value of Object.assign method
What does Object.assign()
return when used?
Object.assign() returns the modified target object.
The Object.assign()
method copies all enumerable own properties from one or more source objects to a target object and returns the modified target object. This means that the target object is updated with the properties from the source objects, and the same target object is returned. This behavior is useful for merging objects or cloning an object by copying its properties into a new object.
const target = { a: 1 };
const source = { b: 2 };
const returnedTarget = Object.assign(target, source);
console.log(returnedTarget); // { a: 1, b: 2 }
console.log(target); // { a: 1, b: 2 }