SyntaxStudy
Sign Up
Home JavaScript Reference Object.assign()

Object.assign()

method ES6

Copies all enumerable own properties from source objects to a target object. Returns the target.

Syntax

Object.assign(target, ...sources)

Example

javascript
const defaults = { theme: 'light', lang: 'en', debug: false };
const userPrefs = { theme: 'dark', notifications: true };

const config = Object.assign({}, defaults, userPrefs);
console.log(config);
// { theme: 'dark', lang: 'en', debug: false, notifications: true }

// Spread alternative (preferred)
const config2 = { ...defaults, ...userPrefs };