Property Override Order
What you'll learn: How the sequence of spread operations determines which property values survive when objects share the same keys.
The Core Idea
When you spread multiple objects that have overlapping property names, JavaScript doesn't throw an error or merge the values—instead, the last spread wins. Properties are assigned left-to-right, so later values simply overwrite earlier ones.
Think of it like painting layers: each new coat of paint covers the previous one. The final visible color is always from the most recent application.
How It Works
const defaults = { theme: 'dark', fontSize: 14 };
const userPrefs = { fontSize: 18 };
const config = { ...defaults, ...userPrefs };
console.log(config);
// { theme: 'dark', fontSize: 18 }
Here, fontSize appears in both objects. The spread happens left-to-right: first defaults adds fontSize: 14, then userPrefs overwrites it with fontSize: 18.
Order Matters
Swap the order, and you get different results:
const config = { ...userPrefs, ...defaults };
console.log(config);
// { fontSize: 14, theme: 'dark' }
Now defaults comes last, so its fontSize: 14 wins.
Practical Pattern
This makes spread perfect for applying defaults with optional overrides:
function createUser(customSettings) {
const defaults = { role: 'guest', active: true };
return { ...defaults, ...customSettings };
}
const admin = createUser({ role: 'admin' });
// { role: 'admin', active: true }
The pattern { ...defaults, ...customSettings } ensures custom values always override defaults.
Key Takeaway: In object spread, properties are applied left-to-right, and the rightmost value for any duplicate key always wins—use this deliberately to control override behavior.