MediumArray & Object Mutations🐛 Debug Challenge

Object.assign first argument is mutated

Buggy Code — Can you spot the issue?

const defaults  = { theme: 'light', lang: 'en' };
const userPrefs = { lang: 'fr' };
const settings  = Object.assign(defaults, userPrefs);

console.log(defaults.lang);
console.log(settings.lang);

Fixed Code

const defaults  = { theme: 'light', lang: 'en' };
const userPrefs = { lang: 'fr' };
const settings  = Object.assign({}, defaults, userPrefs);

console.log(defaults.lang);
console.log(settings.lang);

Bug Explained

Bug: Object.assign(target, ...sources) mutates and returns the target. defaults is the target here — it gets mutated.

Explanation: Using {} as the first argument creates a new merge target. defaults is now a source, not the target — it stays unchanged.

Key Insight: Always use {} as the first argument to Object.assign to avoid mutating sources: Object.assign({}, defaults, overrides).

More Array & Object Mutations Debug Challenges

EasyAssignment copies reference — both variables point to same arrayMediummap returns new array but objects inside are still referencesMediumSpread is shallow — nested array is still shared

Practice spotting bugs live →

38 debug challenges with AI hints

🐛 Try Debug Lab