Object.assign copies the properties of one or more source objects onto a target object and returns that target. It's the classic way to merge objects, apply defaults, or shallow-clone with Object.assign({}, source) — and it copies only what a source owns and exposes.
Implement objectAssign(target, ...sources). For each source (left to right), copy its own enumerable properties — both string-keyed and symbol-keyed — onto target, mutating it. Skip null/undefined sources, let later sources overwrite earlier ones, and return target.
function objectAssign(target, ...sources) {
// mutates target with each source's own enumerable properties;
// returns target.
}
objectAssign({ a: 1 }, { b: 2 }); // { a: 1, b: 2 }
objectAssign({}, { a: 1 }, { a: 2 }); // { a: 2 } — later source wins
objectAssign({ a: 1 }, null, { b: 2 }); // { a: 1, b: 2 } — null skipped
const clone = objectAssign({}, original); // shallow copy of `original`
null and undefined sources contribute nothing (no error).null/undefined target throws a TypeError.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
Object.assign copies the properties of one or more source objects onto a target object and returns that target. It's the classic way to merge objects, apply defaults, or shallow-clone with Object.assign({}, source) — and it copies only what a source owns and exposes.
Implement objectAssign(target, ...sources). For each source (left to right), copy its own enumerable properties — both string-keyed and symbol-keyed — onto target, mutating it. Skip null/undefined sources, let later sources overwrite earlier ones, and return target.
function objectAssign(target, ...sources) {
// mutates target with each source's own enumerable properties;
// returns target.
}
objectAssign({ a: 1 }, { b: 2 }); // { a: 1, b: 2 }
objectAssign({}, { a: 1 }, { a: 2 }); // { a: 2 } — later source wins
objectAssign({ a: 1 }, null, { b: 2 }); // { a: 1, b: 2 } — null skipped
const clone = objectAssign({}, original); // shallow copy of `original`
null and undefined sources contribute nothing (no error).null/undefined target throws a TypeError.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.