JavaScript Object.assign() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Object

Definition

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)
CallWhat 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

ParameterDescription
targetRequired. The object that receives the copied properties. It is changed in place.
sourcesZero or more source objects. Only enumerable own properties are copied. null and undefined sources are skipped.

Return Value

Return ValueDescription
objectThe 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
Try It Yourself

How It Works

  1. Start with a target. target begins with only a.
  2. Copy from source. Object.assign adds b and c onto target.
  3. Same object. The return value is the same object as target, not a brand-new one.

Example 2 — Merge Defaults

Pass an empty object first so defaults and user values merge into a new object.

JavaScript
var defaults = { theme: "light", lang: "en" };
var user = { theme: "dark" };
var settings = Object.assign({}, defaults, user);

console.log(JSON.stringify(settings));  // {"theme":"dark","lang":"en"}
console.log(JSON.stringify(defaults));  // {"theme":"light","lang":"en"}
Try It Yourself

How It Works

  1. New object first. {} is the target, so defaults and user stay unchanged.
  2. Defaults apply. Properties from defaults are copied first.
  3. User wins. Then user.theme overwrites "light" with "dark".

Example 3 — Overwrite Order

When several sources share a key, the last one wins.

JavaScript
var a = { x: 1 };
var b = { y: 2 };
var c = { x: 9, z: 3 };

console.log(JSON.stringify(Object.assign({}, a, b, c)));  // {"x":9,"y":2,"z":3}
Try It Yourself

How It Works

  1. Copy a, then b. After a and b, the object looks like { x: 1, y: 2 }.
  2. c overwrites x. c.x replaces 1 with 9 and adds z.
  3. Order matters. Put higher-priority sources later in the argument list.

Browser & Runtime Support

Object.assign() arrived with ES6 (ES2015) and is Baseline Widely available in modern browsers and Node.js.

✓ Baseline · ES6

Object.assign()

Supported in Chrome 45+, Firefox 34+, Safari 9+, Edge 12+, and Node.js 4+. Safe for everyday apps.

98% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Object.assign Excellent

Bottom line: Use Object.assign() freely in modern browsers. For IE11, use a polyfill or a simple loop.

Frequently Asked Questions

It copies enumerable own properties from one or more source objects into a target object and returns that target.
No. It is a shallow copy. Nested objects and arrays are shared by reference, not cloned.
Yes. Properties are written onto the target. Pass {} as the first argument if you want a new object.
The later source wins. Object.assign({}, { a: 1 }, { a: 2 }) gives { a: 2 }.
No. null and undefined sources are skipped. The target itself must still be a real object.

Next: create()

Create an object with a chosen prototype.

create() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful