Difference Between Object.assign and Spread Operator in JavaScript
Last Updated :
05 Jul, 2024
The Object.assign() and the Spread Operator (...) are commonly used for copying properties from one object to another. While they can achieve similar outcomes, they have distinct characteristics and use cases. This article will explain the differences between the Object.assign() and the spread operator in terms of their functionality, characteristics, and applications providing examples for a better understanding.
These are the following topics that we are going to discuss:
What is Object.assign() ?
The Object.assign() is a method used to copy the values of all enumerable properties from one or more source objects to a target object. It returns the target object.
Characteristics:
- Mutation: The Object.assign() mutates the target object.
- Enumerability: Only copies enumerable properties.
- Prototype Chain: Does not copy properties from prototype chain.
- Shallow Copy: Creates a shallow copy of the source objects.
- Merging Objects: Can be used to merge multiple objects into one.
Applications:
- Copying properties from one object to another.
- Merging multiple objects into the single object.
- Cloning an object.
Example: This example shows the use of Object.assign() method.
JavaScript
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
console.log(returnedTarget);
Output{ a: 1, b: 4, c: 5 }
{ a: 1, b: 4, c: 5 }
What is Spread Operator?
The Spread Operator (...) allows an iterable to be expanded in places where zero or more arguments or elements are expected. When used in the context of the objects, it spreads the properties of the source objects into the new object.
Characteristics:
- Immutability: Creates a new object and does not mutate the original object.
- Enumerability: Only copies enumerable properties.
- Prototype Chain: Does not copy properties from prototype chain.
- Shallow Copy: Creates a shallow copy of source objects.
- Syntax Simplicity: Provides the concise syntax for the copying and merging objects.
Applications:
- Cloning objects.
- Merging multiple objects into the single object.
- Creating new objects with the subset of properties.
Example: This example shows the use of spread operator.
JavaScript
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const merged = { ...obj1, ...obj2 };
console.log(merged);
Output{ a: 1, b: 3, c: 4 }
Difference Between Object.assign and Spread Operator
Characteristics | Object.assign | Spread Operator |
---|
Syntax | Object.assign(target ...sources) | { ...source } |
---|
Mutation | Mutates the target object | Does not mutate the original object |
---|
Prototype Chain | Ignores properties from the prototype chain | Ignores properties from the prototype chain |
---|
Enumerability | Copies enumerable properties | Copies enumerable properties |
---|
Copy Type | Shallow copy | Shallow copy |
---|
Multiple Sources | Can merge multiple source objects | Can merge multiple source objects |
---|
Performance | Slightly slower due to function call | Generally faster due to the concise syntax |
---|
Use Case | Suitable for merging into the existing objects | Suitable for creating new objects |
---|
Conclusion
Both Object.assign() and the spread operator are powerful tools for handling objects in JavaScript. While Object.assign() is useful for the merging properties into the existing object, the spread operator is more suited for creating new objects with the merged properties. Understanding the differences between these two methods will help choose the right one based on the specific use case and performance.
Similar Reads
Difference Between Variables and Objects in JavaScript The variables and objects are fundamental concepts but they serve different purposes. The Variables are used to store data values while objects are used to group related data and functions into a single entity. JavaScript VariableA variable in JavaScript is a named container that stores a value. It
2 min read
Difference between Array and Array of Objects in JavaScript ArrayAn Array is a collection of data and a data structure that is stored in a sequence of memory locations. One can access the elements of an array by calling the index number such as 0, 1, 2, 3, ..., etc. The array can store data types like Integer, Float, String, and Boolean all the primitive dat
3 min read
Difference between forEach() and map() loop in JavaScript The forEach() and map() methods in JavaScript are used to iterate over arrays, but they serve different purposes. forEach() executes a provided function once for each array element without returning a new array, while map() transforms elements and returns a new array.JavaScript forEach() JavaScript'
4 min read
Difference Between Destructuring Assignment and Dot Notation in JavaScript JavaScript offers various ways to access and assign values from the objects and arrays. Two common methods are destructuring assignment and dot notation. Understanding the differences between these methods can help us write more efficient and readable code.These are the following topics that we are
3 min read
What is the difference between call and apply in JavaScript ? JavaScript call() Method: It calls the method, taking the owner object as an argument. The keyword this refers to the 'owner' of the function or the object it belongs to. We can call a method that can be used on different objects. Syntax: object.objectMethod.call( objectInstance, arguments ) JavaScr
2 min read