What does Object.assign() do?
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
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
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
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:
const merged = Object.assign({}, a, b);Cloning an object
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)
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
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
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)
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 |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.