Object.assign()

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

Since ES2015 Spec ↗

Syntax

Object.assign(target, ...sources)

Parameters

NameTypeRequiredDescription
target object Yes The object to copy properties into. It is modified and returned.
sources object No One or more source objects whose enumerable own properties are copied.

Returns

object — The modified target object.

Examples

console.log(Object.assign({ a: 1 }, { b: 2 }, { a: 9 }));
Output
{ a: 9, b: 2 }
const copy = Object.assign({}, { x: 1 });
console.log(copy);
Output
{ x: 1 }

Notes

Mutates and returns the target. Performs a shallow copy - nested objects are shared by reference. Later sources overwrite earlier ones. The spread operator `{ ...a, ...b }` is a common alternative.

Browser & runtime support

EnvironmentSince version
chrome 45
firefox 34
safari 9
edge 12
node 4.0

See also