javascript / beginner
Snippet
Merging Objects with Spread (...)
The spread operator (...) allows you to copy properties from one object to another. Later properties overwrite earlier ones if the keys match.
snippet.js
1
2
3
const baseConfig = { theme: 'light', lang: 'en' };const userPrefs = { theme: 'dark' };const finalConfig = { ...baseConfig, ...userPrefs };
nextjs
Breakdown
1
{ ...baseConfig, ...userPrefs }
Combines both objects. finalConfig will have theme: 'dark' because userPrefs came last.