JavaScript Object assign() Method
Last Updated :
12 Jul, 2024
The Object.assign() method is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target.
Syntax:
Object.assign(target, ...sources);
Parameters:
- target: It is the target object to which values and properties have to be copied.
- sources: It is the source object from which values and properties have to be copied.
Return Value:
Object.assign() returns the target object.
Example 1: In this example, the properties of the object “obj1” i.e. { a: 10 } is copied to the target object “new_obj”.
JavaScript
// creating an object constructor
// and assigning values to it
const obj1 = { a: 1 };
// creating a target object and copying values and
// properties to it using object.assign() method
// Here, obj1 is the source object
const new_obj = Object.assign({}, obj1);
// Displaying the target object
console.log(new_obj);
Output:
Object { a: 1 }Example 2: In this example, the properties of three source objects “obj1, obj2, obj3” are copied to the target object “new_obj”. The value of any pre-existing key-value pair that existed in the previous object will be over-written. For example, obj1.b which has a value of 10 will now be overwritten with obj2.b which has a value of 20
JavaScript
// creating 3 object constructors and assigning values to it
let obj1 = { a: 10 };
let obj2 = { b: 20 };
let obj3 = { c: 30 };
// Creating a target object and copying values
// and properties to it using object.assign() method
let new_obj = Object.assign({}, obj1, obj2, obj3);
// Displaying the target object
console.log(new_obj);
Output :
Object { a: 10, b: 20, c: 30 }Example 3: In this example, the properties of three source objects “obj1, obj2, obj3” are copied to the target object “new_obj” and the target object gets the overwritten values.
JavaScript
// Creating 3 object constructors and assigning values to it
let obj1 = { a: 10, b: 10, c: 10 };
let obj2 = { b: 20, c: 20 };
let obj3 = { c: 30 };
// Creating a target object and copying values and
// properties to it using object.assign() method
let new_obj = Object.assign({}, obj1, obj2, obj3);
// Displaying the target object
console.log(new_obj);
Output:
Object { a: 10, b: 20, c: 30 }Explanation:
In the above code the properties are overwritten by other objects that have the same properties later in the same order of parameters.
Applications:
- Object.assign() is used for cloning an object, to merge objects with the same properties.
Errors and Exceptions:
- A TypeError is raised if the property is non-writable.
- The target object can be changed only if the properties are added before the error is raised.
- Object.assign() does not throw on null or undefined source values
We have a complete list of JavaScript Object methods, to check those please go through this JavaScript Object Complete Reference article.
Supported Browsers:
- Google Chrome
- Mozilla
- Opera
- Safari
JavaScript Object assign() Method- FAQs
How does the assign() method work?
The assign() method copies the properties of one or more source objects into a target object. Properties in the target object are overwritten by properties in the source objects if they have the same key.
Does the assign() method modify the target object?
Yes, the assign() method modifies the target object. It copies properties from the source objects directly into the target object.
How does Object.assign() handle properties that are not enumerable?
Object.assign() only copies enumerable properties from source objects to the target object. Non-enumerable properties are ignored.
How does the assign() method handle null or undefined source objects?
If a source object is null or undefined, it is ignored and skipped during the property copying process. No properties are copied from null or undefined sources.
What are some common use cases for the assign() method?
Common use cases for the assign() method include:
- Merging multiple objects into one.
- Cloning an object (shallow copy).
- Adding properties to an existing object.
- Combining default settings with user-defined settings.
Similar Reads
JS Constructor Method
A constructor method is a function used to initialize a newly created object. It is typically used to set up properties and methods for the object when it is created. A constructor is called automatically when a new instance of an object is created using the new keyword. Function Constructor Method
3 min read
JavaScript Object assign() Method
The Object.assign() method is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target. Syntax:Object.assign(target, ...sources);Parameters:target: It is the target obj
4 min read
JavaScript Object create() Method
JavaScript object.create() method is used to create a new object with the specified prototype object and properties. Object.create() method returns a new object with the specified prototype object and properties. Syntax:Object.create(prototype[, propertiesObject])Parameters:prototype: It is the prot
3 min read
JavaScript Object defineProperty() Method
The Object.defineProperty() method in JavaScript is a Standard built-in object which defines a new property directly on an object or it can also modify the existing property of an object and return the object. Syntax:Object.defineProperty(obj, prop, descriptor)Parameters:This method accepts three pa
3 min read
JavaScript Object defineProperties() Method
The Object.defineProperties() method in JavaScript is a standard built-in Object that defines a new or modifies existing properties directly on an object and it returns the object. Syntax:Object.defineProperties(obj, props) Parameters:Obj: This parameter holds the object on which the properties are
2 min read
JavaScript Object entries() Method
The Object.entries() method in JavaScript is used to retrieve an array of an object's enumerable property [key, value] pairs. This method is particularly useful for transforming and iterating over objects in situations where array-like manipulation is needed. Syntax:Object.entries(obj);Parameters:ob
4 min read
JavaScript Object freeze() Method
The Object.freeze() method is used to freeze an object. Freezing an object does not allow new properties to be added to the object and prevents removing or altering the existing properties. Object.freeze() preserves the enumerability, Configurability, writability, and prototype of the object. It ret
3 min read
JavaScript Object getOwnPropertyDescriptor() Method
The Object.getOwnPropertyDescriptor() method in JavaScript is a standard built-in object that enables the full information on a property to be accessed and returns a property descriptor for the own property of a given object. Syntax: Object.getOwnPropertyDescriptor( obj, prop ) Parameters: This meth
2 min read
JavaScript Object getOwnPropertyNames() Method
The Object.getOwnPropertyNames() method in JavaScript is a standard built-in object which returns all properties that are present in a given object except for those symbol-based non-enumerable properties. Syntax:Object.getOwnPropertyNames(obj)Parameters:This method accepts a single parameter as ment
3 min read
JavaScript Object getOwnPropertySymbols() Method
The Object.getOwnPropertySymbols() method in JavaScript is a standard built-in object which returns an array of all symbol properties that are present in a given object. An empty array is returned until symbol properties are set on the object. Syntax: Object.getOwnPropertySymbols(obj) Parameters: ob
2 min read