Implement Object.assign()

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement Object.assign()

Approach

Iterate over each source object, copying its own enumerable string-keyed properties to the target using Object.keys, and its enumerable symbol-keyed properties using Object.getOwnPropertySymbols. Skip null/undefined sources and throw on null/undefined target.

Complexity Analysis

Time
O(n) where n is the total number of properties across sources
Space
O(1) beyond the target object

Pattern

Shallow Copy

Why It Works

Copying only own enumerable properties with Object.keys matches the native Object.assign specification, and later sources naturally overwrite earlier ones for the same key.

Updated Feb 2026