JavaScript (ES2026) reference · Object Methods
JavaScript Object.assign() Method
Copy all own enumerable properties from one or more source objects into a target object, and return the target.
JavaScript (ES2026) Since ES2015
Syntax
Object.assign(target, ...sources)
Parameters
| Parameter | Description |
|---|---|
target |
The object to copy into (it is changed). |
sources |
Objects to copy from; later ones win. |
Return value
The target object.
Examples
const defaults = { theme: "light", lang: "en" };
const settings = Object.assign({}, defaults, { lang: "de" });
console.log(settings, defaults);
Output
{ theme: 'light', lang: 'de' } { theme: 'light', lang: 'en' }const a = { nested: { x: 1 } };
const b = Object.assign({}, a);
b.nested.x = 99;
console.log(a.nested.x); // shallow copy: nested objects are shared
console.log({ ...a, extra: true });
Output
99
{ nested: { x: 99 }, extra: true }Version history
| Version | Change |
|---|---|
| JavaScript ES2015 | Added |
In other languages
dict.update()· Pythonarray_merge()· PHParray_replace()· PHP
Related
Object.entries()· Return an array of an object's own enumerable [key, value] pairs, ready for for…of loops or array methods.Object.fromEntries()· Build an object from an iterable of [key, value] pairs, such as an array or a Map. The opposite of Object.entries().structuredClone()· Create a deep copy of a value, including nested objects, arrays, Dates, Maps and Sets. Functions can't be cloned.- JavaScript Objects · tutorial