JavaScript (ES2026) reference · Object Methods

JavaScript Object.assign() Method

Copy all own enumerable properties from one or more source objects into a target object, and return the target.

JavaScript (ES2026) Since ES2015

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Object.assign(target, ...sources)

Parameters

ParameterDescription
target The object to copy into (it is changed).
sources Objects to copy from; later ones win.

Return value

The target object.

Examples

Merge with defaultsTry it yourself
const defaults = { theme: "light", lang: "en" };
const settings = Object.assign({}, defaults, { lang: "de" });
console.log(settings, defaults);

Output

{ theme: 'light', lang: 'de' } { theme: 'light', lang: 'en' }
Shallow copy, and the spread alternativeTry it yourself
const a = { nested: { x: 1 } };
const b = Object.assign({}, a);
b.nested.x = 99;
console.log(a.nested.x); // shallow copy: nested objects are shared
console.log({ ...a, extra: true });

Output

99
{ nested: { x: 99 }, extra: true }

Version history

VersionChange
JavaScript ES2015Added

In other languages

Was this page helpful?