Question Bank

JavaScript Merge Two Objects: Three Approaches Quiz

Difficulty: Medium

Three seeded ways to merge two plain objects (spread, `Object.assign`, and a manual `for...in` copy), plus two companions on shallow-vs-deep semantics and on which approaches mutate the target.

Question Bank
/

JavaScript Merge Two Objects: Three Approaches Quiz

JavaScript Merge Two Objects: Three Approaches Quiz

Three seeded ways to merge two plain objects (spread, `Object.assign`, and a manual `for...in` copy), plus two companions on shallow-vs-deep semantics and on which approaches mutate the target.

Question Bank
Medium
JavaScript
5 questions
quiz
references
js-spread-rest
interview-prep

549 views

17

Implement merge(obj1, obj2) using the spread operator so it returns a new object containing all properties from obj1 and obj2. The original inputs must not be mutated, and obj2 keys should win on collision.

Examples

Example 1:

Input: merge({a: 1, b: 2}, {c: 3, d: 4, e: 5})
Output: { a: 1, b: 2, c: 3, d: 4, e: 5 }
Explanation: spread copies own enumerable keys from each source into a fresh object, in the order they appear.