Use Object.assign() or spread operator for shallow copies
const original = { a: 1, b: 2 };
const copy1 = Object.assign({}, original);
const copy2 = { ...original };
console.log(copy2); // { a: 1, b: 2 }const defaults = { theme: 'light', lang: 'en' };
const userPrefs = { lang: 'fr', fontSize: 14 };
// Merge objects (right overrides left)
const config = Object.assign({}, defaults, userPrefs);
console.log(config);
// Add properties while copying
const enhanced = Object.assign({}, original, {
timestamp: Date.now(),
version: '1.0'
});
// Multiple sources
const merged = Object.assign({}, obj1, obj2, obj3);const copy = Object.assign({}, original, {
modified: true,
timestamp: Date.now()
});const copied = arr.map(obj => ({ ...obj }));Avoid thinking it creates deep copies
// DON'T DO THIS - nested objects are still referenced
const copy = Object.assign({}, { nested: { val: 1 } });
copy.nested.val = 2; // Affects original!✓ Works in all modern browsers (ES2015+)