Shallow Copy in JavaScript17 Mar 2025 | 4 min read A bit-wise copy of an object is known as 'Shallow Copy'. When there is an original object, a new object is created where the same values as in the original object are copied. To know about Shallow copy, one should know about the types of data types. In this section, we will take a brief introduction of data types and then understand the Shallow copy in JavaScript. We will also look at an example that will tell how shallow copy works. Data Types in JavaScriptBasically, there are two types of Data types given below:
We will understand its role in the shallow copy. What is Shallow CopyThe shallow copy of an object refers to the reference location/address of the original object. In case any field of the object reference to the other objects, it copies only the reference address (i.e., the memory address) of the object and no new object is created. This is how a shallow copy is different from a deep copy. The shallow copy replicates the object's top-level properties, but the nested object is shared between the original and the copied one. A shallow copy only copies the collection structure and not the values. It means two collections share the elements of the original collection. Generally, a shallow copy is easy and simple to use because no object is created, only the reference address is copied. Not only in the case of arrays, but the same applies to the arrays too. Thus, in the case of primitive data types, when we create a copy of the primitive data type variable, the value is copied to a new memory address to which the new variable points. So, whenever we make a copy, it will create a new memory location and will be a real copy of the variable. Thus, if we execute c = a, then a copy of 'a' is created. So, if we try to assign a new value to c, the value of c will change, but it will not affect the value stored in variable a. It is because once initiated, values are stored for once. In the case of reference data types, it stores the memory address of the object (i.e., location where the object is stored). Thus, copying of the values works well in the reference data types. So, both shallow and deep copies are types of reference data types only. Now, we will try to understand Shallow copy with the help of an example. Example of Shallow CopyConsider two objects, obj1 and obj2, where obj2 reference to obj1. Hence, obj2 will copy the same memory address as the obj1, which means both the objects will have the same memory address and will point to the same address only. You can see in the below diagram that represents the example well: ![]() Example Code ImplementationBelow we have mentioned a code example where we have created a shallow copy for the details of the student: Output: The output of the above code is shown below: ![]() Code Explanation:
Thus, in this way, the shallow copy of one object can be created and used for assigning different values for the object properties. |
In this article, we are going to discuss JavaScript animation with its functions. What is ? JavaScript animations are done by incremental programming changes in the style of an element. JavaScript animations can perform tasks that CSS cannot. JavaScript could be used to transfer several DOM elements...
7 min read
An Http cookie, also known as a web cookie, is a small piece of data that the server sends to the web browser. The browser may store the cookies and send them back to the server with other requests. The typical use of the cookies...
10 min read
The JavaScript Console is provided easy methods with a quick and effective way to run JavaScript code just inside the browser. It is frequently used for many different things, such as logging some code's output or debugging code via the Console. In addition to the...
9 min read
To click a link on a web page, we need to trigger a user action on the click event. But what if we want to click a link pro grammatically using JavaScript without a user event or any other phenomenon? We could accomplish this using JavaScript. There...
4 min read
JavaScript is a potent scripting language that gives programmers the ability to make dynamic, interactive web sites. Finding a specific element's parent element is a frequent operation when working with the Document Object Model (DOM). Here, we'll look at a variety of JavaScript-based approaches to accomplishing...
6 min read
In JavaScript, the interaction with the HTML elements is handled through the Events. These events can be user events or browser events. Any change in the object's state due to some browser or user interaction or activity is known as an event. There are many...
5 min read
? A checkbox is a selection box that allows the users to make the binary choice (true or false) by checking and unchecking it. Basically, a checkbox is an icon, which is frequently used in GUI forms and application to get one or more inputs from...
5 min read
In this section, we will take an introduction of the queue and also see how to implement a queue in JavaScript. What is Queue? A queue is a data structure where the data is organized in the form of a queue. We can understand a queue similar...
6 min read
Window.location is used to provide a Location object with details about the document's current location. This Location object reflects the location (URL) of the object it's linked to, i.e., it holds information about the current content location (host, href, etc.) JavaScript Location Object The window.location attribute represents...
8 min read
JS First Class Function JavaScript is a dynamic scripting language that allows us to use it in the form of an object-oriented style or functional style. In the same way, one such feature of JavaScript is the First class function. In this section, we will discuss the...
4 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