JavaScript Copy Object7 Apr 2025 | 4 min read We will comprehend the JavaScript copy object in this article. There are various ways to copy objects in JavaScript which are given below:
We will learn about each method properly one by one. Utilizing the spread (...) syntaxWe can copy objects in JavaScript with the help of the spread (...) operator. Demonstration:We will utilize the spread (...) operator in this demo. Code: Output: We can witness that the object is copied utilizing the spread (...) operator. We have printed both objects, "person" and "p1" to the console. The "person" is the original object and "p1" is the copied object of "person" which is why they both are identical. ![]() Utilizing the Object.assign() methodWe can copy objects in JavaScript with the help of the Object.assign() method. Demonstration:We will utilize the Object.assign() method in this demo. Code: Output: We can witness that the object is copied utilizing the Object.assign() method. We have printed both objects "person" and "p2". The "person" is the original object and the "p2" is the copied object of "person". ![]() Utilizing the JSON.stringify() and JSON.parse() methodsWe can copy objects in JavaScript with the help of the JSON.stringify() and JSON.parse() methods. Demonstration:We will utilize the JSON.stringify() and JSON.parse() methods in this demo. Code: Output: We can witness that the object is copied utilizing the JSON.stringify() and JSON.parse() methods. We have printed both objects "person" and "p3". The person is the original object and "p3" is the copied object of "person". ![]() Both spread (...) and Object.assign() creates a shallow copy whereas JSON methods creates a deep copy. What is a shallow copy?A shallow copy stores a new object which is the copy of the original object with a new reference and stores only the reference address of the object. A shallow copy only copies the top-level properties of the original object. The nested objects are shared between the original object and cloned object that means both original object and cloned object are dependent of each other. What is a deep copy?A deep copy constructs a new object which is an exact so it copies and stores the copy of all members of the original object that consists of all properties and nested objects. The nested objects are not shared between the original object and cloned object which means both original object and cloned object are independent of each other. Demonstration of a shallow copy:Let us comprehend how to construct a shallow copy with the help of demonstrations. Demo: We have created a shallow copy with the help of Object.assign() method in the following demo. Code: Explanation: We have created an object called "person" and copied it utilizing Object.assign() method then we have changed the firstName, street and city. After that, we have printed the copied object to the console. We can witness that the values of firstName, address-street and address-city of the copied object is changed. Now, we will print the original object to the console utilizing the following code. We can witness that the values of address-street and address-city of the original object is changed. The value of firstName is not changed because the firstName is a primitive value and other address values are the reference value. ![]() Demonstration of a deep copy:Let us comprehend how to construct a deep copy with the help of demonstrations. Demo 1: We have created a shallow copy with the help of the JSON.stringify() and JSON.parse() methods in the following demo. Code: Explanation: We have created an object called "product" and copied it utilizing JSON.parse() and JSON.stringify() methods then we have changed the productName, description-processor and description-memory. After that, we have printed the copied object to the console. We can witness that the values of productName, description-processor and description-memory of the copied object is changed. ![]() Now, we will print the original object to the console utilizing the following code. We can witness that the values of the original object are not changed because the original object and the copied object are disconnected from each other. ![]() Conclusion:We have understood the JavaScript copy object in this article. We have comprehended three ways to copy an object in JavaScript. The ways are utilizing the spread (...) method, the Object.assign() method, the JSON.stringify() & JSON.parse() methods. Next TopicJavascript-code-for-insertion-sort |
It is important to understand the concept of DOM when working with HTML objects in JavaScript since DOM is the structure of the document that defines the relationships between different parts of the webpage. Hierarchy of DOM is represented as a tree of the objects....
6 min read
What is the Join() method in JavaScript? In JavaScript, the join() method is used to join the elements of an array into a string. The elements of the string will be separated by a specific separator and its default value is comma(,). In simple words, with the use...
4 min read
What is WebAssembly (Wasm)? Wasm, short for WebAssembly, is the binary instruction format used by compilers to turn high-level languages like C, Rust, and C++ into low-level machine code that the browser can then execute or whatever it is that you are compiling for. By letting developers...
17 min read
JavaScript is a perfect programming language that is mostly used in web development to enhance capability and user response. The main function of JavaScript in this regard is to swap image sources. Furthermore, descriptive features of images-like contrast, saturation, brightness, etc. are all performed by...
9 min read
In JavaScript, adding elements to an array or list can be done in many ways. Every method has its unique application and easy accessibility. Every method is useful and choosing the best one makes the problem optimized. Let us see the methods that are used to...
4 min read
Logical AND (&&) Operator in JavaScript Logical operators are a crucial part of JavaScript, a strong and adaptable computer language used for web development. One of the basic logical operators in JavaScript for carrying out logical operations is the logical AND (&&) operator. If all of the...
6 min read
JavaScript applications that can be saved as a bookmark's URL in a web browser are referred to as bookmarklets or JavaScript bookmarks. JavaScript bookmarks run code within the current internet web page, as antagonistic to conventional bookmarks, which most effectively store a webpage's URL for convenient...
6 min read
There is no need to wait for style sheets, graphics, or sub frames to finish loading before the DOMContentLoaded event occurs after the main HTML content has been fully loaded and processed. The event is non-cancelable and generic in JavaScript. When referencing elements in the body...
3 min read
We will understand the online JavaScript compiler GDB in this article. Online JavaScript Compiler The online JavaScript compiler is also called an online code editor or an online code playground. It permits users to write, run and test JavaScript code. The online compilers are easy to use...
3 min read
Introduction: The technique of flattening an array reduces the array's n dimensions to 1 dimension. In simple words, it means to combine several nested arrays into a single array to create a single, one-dimensional array. Just decreasing an array's dimensions is known as flattening it. The method...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India