Suggest an editImprove this articleRefine the answer for “What does Object.assign() do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)`Object.assign(target, ...sources)` copies properties from one or more source objects into the target object `target`, overwriting matching keys, and returns `target` itself. It mutates `target`, so to copy without mutation, use an empty object as the first argument: `Object.assign({}, obj)`. **Key point:** `Object.assign()` only creates a shallow copy - nested objects are copied by reference, not duplicated.Shown above the full answer for quick recall.Answer (EN)Image`Object.assign()` is a built-in JavaScript method used to **copy properties from one or more objects into another object**. It lets you **merge objects** and **create shallow copies**. --- ## Syntax ```javascript Object.assign(target, ...sources) ``` | Parameter | Description | |---|---| | `target` | The object **that properties are copied into** | | `sources` | One or more **source** objects that properties are taken from | Returns `target` itself. --- ## A simple example ```javascript const user = { name: 'Alice' }; const details = { age: 25, city: 'Kyiv' }; Object.assign(user, details); console.log(user); // { name: 'Alice', age: 25, city: 'Kyiv' } ``` All the properties from `details` are **copied into** `user`. If keys match, the values are **overwritten**. --- ## Example with property overwriting ```javascript const a = { name: 'Alice', age: 25 }; const b = { age: 30, city: 'Kyiv' }; const result = Object.assign(a, b); console.log(result); // { name: 'Alice', age: 30, city: 'Kyiv' } console.log(a === result); // true (a is changed) ``` Important: `Object.assign()` **mutates the first argument (**`target`**)**. If you don't want to mutate the object, pass an empty object as the first parameter: ```javascript const merged = Object.assign({}, a, b); ``` --- ## Cloning an object ```javascript const user = { name: 'Alice', city: 'Kyiv' }; const clone = Object.assign({}, user); console.log(clone); // { name: 'Alice', city: 'Kyiv' } console.log(clone === user); // false (different objects) ``` This creates a **shallow copy**: nested objects are copied **by reference**, not duplicated. --- ## Example with a nested object (shallow copy) ```javascript const user = { name: 'Alice', address: { city: 'Kyiv' } }; const clone = Object.assign({}, user); clone.address.city = 'Lviv'; console.log(user.address.city); // "Lviv" - it's the same reference! ``` That is, `Object.assign()` does a **shallow copy**, not a deep one. For a deep copy, use `structuredClone()` or `_.cloneDeep()` instead. --- ## Example of merging several sources ```javascript const a = { x: 1 }; const b = { y: 2 }; const c = { z: 3 }; const merged = Object.assign({}, a, b, c); console.log(merged); // { x: 1, y: 2, z: 3 } ``` Lets you merge **several objects into one**. --- ## You can copy values from primitives ```javascript const target = {}; Object.assign(target, { a: 1 }, 'abc', null); console.log(target); // { '0': 'a', '1': 'b', '2': 'c', a: 1 } ``` The string `"abc"` behaves like an array of characters, each letter becomes a property `'0'`, `'1'`, `'2'`. --- ## Merging with existing objects (a common pattern) ```javascript const settings = { darkMode: false, lang: 'en' }; const userSettings = { darkMode: true }; const final = Object.assign({}, settings, userSettings); console.log(final); // { darkMode: true, lang: 'en' } ``` Often used to "merge default settings" with user ones. --- ## Summary | What it does | Example | Behavior | |---|---|---| | Copies properties from one object into another | `Object.assign(a, b)` | Mutates `a` | | Creates a shallow copy | `Object.assign({}, obj)` | Does not mutate the source | | Merges several objects | `Object.assign({}, a, b, c)` | Later ones overwrite earlier ones | | Does not do a deep copy | - | Nested objects are copied by reference |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.