<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: João Juliasz de Morais</title>
    <description>The latest articles on DEV Community by João Juliasz de Morais (@joao_juliasz).</description>
    <link>https://dev.to/joao_juliasz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1207652%2F8978a31b-e834-414c-bcde-98dd44d994b0.jpeg</url>
      <title>DEV Community: João Juliasz de Morais</title>
      <link>https://dev.to/joao_juliasz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joao_juliasz"/>
    <language>en</language>
    <item>
      <title>Shallow copy and Deep copy in JavaScript</title>
      <dc:creator>João Juliasz de Morais</dc:creator>
      <pubDate>Sun, 12 Nov 2023 00:39:28 +0000</pubDate>
      <link>https://dev.to/joao_juliasz/shallow-copy-and-deep-copy-in-javascript-226i</link>
      <guid>https://dev.to/joao_juliasz/shallow-copy-and-deep-copy-in-javascript-226i</guid>
      <description>&lt;p&gt;An important concept to know as a javascript developer is the difference between shallow copy and deep copy.&lt;br&gt;
First, let's talk how object works in javascript.&lt;/p&gt;

&lt;p&gt;An object is a dynamic data type that can take in collections of key-value pairs, where each key corresponds to a property and each value represents the property's associated data.&lt;br&gt;
Objects can hold various data types, including numbers, string, booleans, array, other objects, and even functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shallow copy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A shallow copy of an object is a copy whose properties share the same references as those of the source object from which the copy was made. In simple words, both objects point to the same address in memory. So, when you change either source, or the copy, you may cause changes in the other object too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sourceObj = {name: "Jane", surname: "Doe"}
const copyObj = sourceObj

copyObj.name = "Joe"

console.log(sourceObj) // {name: "Joe", surname: "Doe"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Deep copy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On the other hand, a deep copy is a copy whose properties do not share the same references as those of the source object from which the copy was made. So, all the properties from the source object will be in the copy object, but the new object will be pointed to a different address in memory. In this way, both objects are independent of each other in case of modification. In Javascript we can use JSON.parse() and JSON.stringify() methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sourceObj = {name: "Jane", surname: "Doe"}
const copyObj = JSON.parse(JSON.stringify(sourceObj))

copyObj.name = "Joe"

console.log(sourceObj) // {name: "Jane", surname: "Doe"}
console.log(copyObj) // {name: "Joe", surname: "Doe"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>softwareengineering</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
