Object.assign()
Object.assign(target, ...sources) Returns:
object · Updated March 15, 2026 · Object Methods object assign copy merge
Object.assign() copies all enumerable own properties from one or more source objects to a target object and returns the modified target. It’s commonly used for shallow cloning and merging objects.
Syntax
Object.assign(target, ...sources)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
target | object | — | The target object to receive properties |
sources | object | — | One or more source objects to copy from |
Examples
Basic object merging
const target = { a: 1 };
const source = { b: 2 };
const result = Object.assign(target, source);
console.log(result);
// { a: 1, b: 2 }
console.log(target);
// { a: 1, b: 2 } — target is mutated!
Merging multiple sources
const merged = Object.assign(
{ name: "JavaScript" },
{ version: "ES2024" },
{ feature: "top-level await" }
);
console.log(merged);
// { name: "JavaScript", version: "ES2024", feature: "top-level await" }
Shallow cloning an object
const original = { a: 1, b: 2 };
const clone = Object.assign({}, original);
console.log(clone);
// { a: 1, b: 2 }
console.log(clone === original);
// false
Common Patterns
Setting default options:
const defaults = { theme: "light", lang: "en" };
const userOptions = { theme: "dark" };
const options = Object.assign({}, defaults, userOptions);
console.log(options);
// { theme: "dark", lang: "en" }
Avoiding mutation — always use an empty {} as the target if you don’t want to mutate the original:
const obj = { x: 1 };
Object.assign(obj, { y: 2 }); // mutates obj — often unintended
See Also
- Object.create — Create object with prototype
- Object.freeze — Make object immutable
- Object.keys — Get object property names