const defaults = { theme: 'light', lang: 'en' };
const userPrefs = { lang: 'fr' };
const settings = Object.assign(defaults, userPrefs);
console.log(defaults.lang);
console.log(settings.lang);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: 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).