Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript Object.assign() Method

  • 18 Sep, 2025
  • Com 1
JavaScript Object.assign() Method

JavaScript Object.assign() is a built-in static method that copies all enumerable own properties from one or more source objects to a target object. It facilitates object merging, shallow cloning, and property copying in a concise manner.

JavaScript Object.assign() to merge objects

const target_obj = { mobile: 'iPhone17' };
const source_obj = { ios: 26, chip: 'A19' };

Object.assign(target_obj, source_obj);

console.log(target_obj);

// Output: { mobile: 'iPhone17', ios: 26, chip: 'A19' }

In this code, we defined two objects: source_obj and target_obj. We want to modify the target_obj by cloning the properties of the source_obj.

We merged the target_obj with the source_obj using the assign() method, and after merging, the target_obj now has three properties.

Syntax

Object.assign(target_obj, ...source_obj)

Parameters

Argument Description
target_obj (required)

It is the object to which properties will be copied.

It can be any value, but if it is not an Object, it will be converted to one.

source_obj (optional)

It represents zero or more source objects from which enumerable own properties are copied.

If a source object is null or undefined, it is ignored without throwing an error.

Merging multiple sources

We can merge as many objects as we want using the .assign() method.

Merging multiple objects in JavaScript

const target_obj = { mobile: 'iPhone17' };
const source_one = { ios: 26, chip: 'A19' };
const source_two = { camera: '48px', feature: 'AI' }

Object.assign(target_obj, source_one, source_two);

console.log(target_obj);

// Output:
// {
//   mobile: 'iPhone17',
//   ios: 26,
//   chip: 'A19',
//   camera: '48px',
//   feature: 'AI'
// }

In this code, we merged the target_obj with the source_one and source_two objects. Properties are copied sequentially. Ideal for combining multiple configurations into a single one.

Overwriting properties

When merging multiple objects, if multiple source objects contain the same property key, the value from the last source object will overwrite earlier ones in the target object.

Overwriting properties in JS

const target_obj = { mobile: 'iPhone17' };
const source_one = { ios: 26, chip: 'A19' };
const source_two = { camera: '48px', ios: 27 } // Overwrites 'ios' from source_one

Object.assign(target_obj, source_one, source_two);

console.log(target_obj);

// Output:
// {
//   mobile: 'iPhone17',
//   ios: 27,
//   chip: 'A19',
//   camera: '48px',
// }

In this code, source_one has a property called ios: 26. 

Similarly, source_two also has the same property, ios: 27, with a value of 27.

That means when you merge objects, property ios: 27 will win because it will overwrite the ios: 26. And hence, the output object contains ios: 27.

Shallow cloning (Nested objects are not cloned)

The Object.assign() method creates a shallow copy of the specified object.

Nested objects are not deeply cloned; instead, their references are shared between the original and the clone, so mutations in the nested structure affect both.

In other words, if you change the nested object of the cloned object, the nested object of the original object will be changed too due to the shared reference.

const original = { a: 1, nested: { b: 2 } };

console.log(original.nested.b)

// Output: 2

const clone = Object.assign({}, original);

clone.nested.b = 3;

console.log(clone.nested.b)

// Output: 3

console.log(original.nested.b);

// Output: 3 (Original object's "b" property is also changed to 3)

In this code, we first cloned the original object, and now we have a clone object.

Then, we modified the clone object’s nested “b” property. Ideally, it should not affect the original object’s “b” property, but in this case, even the original object’s “b” property is changed too! This is called shallow copying.

To deep clone an object, use JSON.parse(JSON.stringify()) (with limitations) or the structuredClone() method.

Deep Clone with structuredClone() (Modern approach)

JavaScript structuredClone() method of the Window interface creates a deep clone of a given value using the structured clone algorithm.

const original = { a: 1, nested: { b: 2 } };

console.log(original.nested.b);

// Output: 2

const deepClone = structuredClone(original);

deepClone.nested.b = 3;

console.log(deepClone.nested.b)

// Output: 3 (Cloned object's nested value)

console.log(original.nested.b);

// Output: 2 (Original object's nested b's value is unchanged)

As you can see, it maintains the object structure more accurately.

This is the most preferred method for cloning an object in JavaScript.

That’s all!

Post Views: 6
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Python List remove(): Deleting Elements from a List
JavaScript Object.values() Method

1 Comment

  1. ngoc danh

    February 9, 2020 at 2:02 pm

    tks friend

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend