Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Merge Two Objects

Data Structures, Operators & Stringsmedium

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 118: Enhanced Object Literals

Implement `merge(a, b)` returning a new object combining both, where keys in `b` override keys in `a`.

Sample tests

Input: merge({"a":1}, {"b":2})
Output: {"a":1,"b":2}
Input: merge({"a":1}, {"a":2})
Output: {"a":2}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Object.assign onto a mutates a; assign into {} or use spread.

Learning resources

  • MDN: Object
Approach & explanation (try first)

{ ...a, ...b } shallow-merges with b overriding a, without mutating either input.

Loading...
⌘/Ctrl + Enter

Run your code to see results.