Object.assign(target, ...sources) copies enumerable own properties from one or more source objects into target. Later sources overwrite earlier ones when keys match.
Object is built into JavaScript. Call the method on Object itself. It returns the same target object after the copy.
Syntax
Call Object.assign() like this. Start with this line.
JavaScript
Object.assign(target, ...sources)
Call
What it does
Object.assign(target, ...sources)
Copies enumerable own properties from each source into target and returns target.
Built into JavaScript. Call on Object — no import required.
Library declarations
JavaScript
Object.assign(target, ...sources)
JavaScript
assign(target: object, ...sources: any[]): any;
Parameters
Parameter
Description
target
Required. The object that receives the copied properties. It is changed in place.
sources
Zero or more source objects. Only enumerable own properties are copied. null and undefined sources are skipped.
Return Value
Return Value
Description
object
The same target object, after the properties have been copied.
JavaScript Examples
Example 1 — Basic Use
Copy properties from a source into a target. The target is updated and returned.
JavaScript
var target = { a: 1 };
var source = { b: 2, c: 3 };
var result = Object.assign(target, source);
console.log(JSON.stringify(result)); // {"a":1,"b":2,"c":3}
console.log(JSON.stringify(target)); // {"a":1,"b":2,"c":3}
console.log(result === target); // true