Sub topic 8
Spreading Objects to Copy and Merge
This chapter assumes you know how to create plain objects, read their properties, and use spread in array literals from the previous chapter. Object spread uses the same ... syntax inside { }. It copies an object's properties into a new object literal.
Copying an Object
Writing ...source inside an object literal copies every own enumerable property of source into the new object:
const point = { x: 1, y: 2 };
const copy = { ...point };
console.log(copy); // { x: 1, y: 2 }
console.log(copy === point); // false (a new object)The copy is a separate object, so adding or changing a top-level property on copy leaves point alone. Spread copies only own properties. It ignores inherited properties and the prototype, so the result is always a plain object, even when the source was a class instance.
Merging: Later Properties Win
You can spread several objects and mix in regular properties. JavaScript processes the literal from left to right. When a key appears more than once, the last value written wins.
const defaults = { theme: 'light', fontSize: 14, autosave: true };
const userPrefs = { fontSize: 18 };
const settings = { ...defaults, ...userPrefs, theme: 'dark' };
console.log(settings);
// { theme: 'dark', fontSize: 18, autosave: true }Order is the most common source of bugs here. Put defaults first and overrides last. If you write { theme: 'dark', ...defaults }, then defaults.theme overwrites your 'dark'.
Spreading Unusual Values
Object spread tolerates values that array spread would reject:
| Expression | Result | Why |
|---|---|---|
| `{ ...null }` | `{}` | `null` and `undefined` are ignored |
| `{ ...undefined }` | `{}` | Same rule |
| `{ ...false }` | `{}` | Primitives with no own enumerable properties add nothing |
| `{ ...'hi' }` | `{ 0: 'h', 1: 'i' }` | A string has indexed own properties |
| `{ ...[10, 20] }` | `{ 0: 10, 1: 20 }` | An array's indexes are its keys |
Because ignoring falsy values is safe, you can use spread to add a property only when a condition holds:
const isAdmin = false;
const user = {
name: 'Ada',
...(isAdmin && { permissions: ['all'] }),
};
console.log(user); // { name: 'Ada' }When isAdmin is false, the expression spreads false and adds nothing. When it is true, it spreads { permissions: ['all'] }.
Getters Become Plain Values
Spread reads each property, so a getter runs once and its result is stored as an ordinary data property:
const source = {
get now() { return 'computed'; },
};
const copy = { ...source };
console.log(Object.getOwnPropertyDescriptor(copy, 'now').value); // 'computed'The copy has no getter. It holds a snapshot of the value.
Exercise
What does this log? const base = { a: 1, b: 2 }; const extra = { b: 3, c: undefined }; console.log({ c: 0, ...base, ...extra });
Show answer
{ c: undefined, a: 1, b: 3 }. The literal sets c to 0 first. Spreading base adds a: 1 and b: 2. Spreading extra then overwrites b with 3 and c with undefined, because spread copies undefined values too. Key order follows first insertion, so c stays first.
Exercise
Write an expression that builds { id: 5 } and adds tag: 'new' only when the variable isNew is true.
Show answer
{ id: 5, ...(isNew && { tag: 'new' }) }. When isNew is false, spreading false adds nothing.