JavaScript Interview Question #34
What is Object.assign()?
Objects, Arrays & Core Data Handling Junior Beginner
Quick Interview Answer
Object.assign copies enumerable own properties from one or more source objects to a target object and returns the target. Later sources overwrite earlier properties with the same key.
Detailed Explanation
Object.assign copies enumerable own properties from one or more source objects to a target object and returns the target. Later sources overwrite earlier properties with the same key.
The copy is shallow. Nested objects remain shared references unless they are copied separately. Object spread is often used as a more readable alternative for simple object copying and merging.
Code Example
const defaults = { theme: "light", pageSize: 10 };
const options = { pageSize: 25 };
const settings = Object.assign({}, defaults, options);
console.log(settings);
// { theme: "light", pageSize: 25 }