<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[shallow and deep]]></title><description><![CDATA[shallow and deep]]></description><link>https://shallowanddeepssk.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 19 Oct 2026 06:08:46 GMT</lastBuildDate><atom:link href="https://shallowanddeepssk.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Rest , Spread ,Deep Copy ,  Shallow Copy...]]></title><description><![CDATA[Mastering Rest and Spread Operators in JavaScript (With Deep vs Shallow Copy)
JavaScript is full of powerful features, and two of the most useful are the Rest (...rest) and Spread (...spread) operators. These operators simplify working with arrays, o...]]></description><link>https://shallowanddeepssk.hashnode.dev/rest-spread-deep-copy-shallow-copy</link><guid isPermaLink="true">https://shallowanddeepssk.hashnode.dev/rest-spread-deep-copy-shallow-copy</guid><dc:creator><![CDATA[Suram shiva Krishna]]></dc:creator><pubDate>Sun, 23 Feb 2025 17:17:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1740330951565/86bb242b-7414-4b1a-a48c-6bc907a0b6e7.avif" length="0" type="image/jpeg"/><content:encoded><![CDATA[<hr />
<h1 id="heading-mastering-rest-and-spread-operators-in-javascript-with-deep-vs-shallow-copy">Mastering Rest and Spread Operators in JavaScript (With Deep vs Shallow Copy)</h1>
<p>JavaScript is full of powerful features, and two of the most useful are the <strong>Rest (</strong><code>...rest</code>) and Spread (<code>...spread</code>) operators. These operators simplify working with arrays, objects, and function arguments. However, their behavior can lead to confusion when dealing with <strong>shallow and deep copies</strong>. In this article, we’ll break down these concepts with practical examples.</p>
<hr />
<h2 id="heading-what-is-the-rest-operator-rest">What is the Rest Operator (<code>...rest</code>)?</h2>
<p>The <strong>rest operator (</strong><code>...</code>) collects multiple values into an <strong>array</strong>. It is commonly used in <strong>function parameters</strong> and <strong>object/array destructuring</strong>.</p>
<h3 id="heading-example-1-using-rest-in-function-parameters">Example 1: Using Rest in Function Parameters</h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sum</span>(<span class="hljs-params">...numbers</span>) </span>{
    <span class="hljs-keyword">return</span> numbers.reduce(<span class="hljs-function">(<span class="hljs-params">acc, num</span>) =&gt;</span> acc + num, <span class="hljs-number">0</span>);
}

<span class="hljs-built_in">console</span>.log(sum(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>)); <span class="hljs-comment">// Output: 15</span>
</code></pre>
<p>How it works? The <code>...numbers</code> parameter collects all function arguments into an array, allowing us to apply the <code>.reduce()</code> method.</p>
<hr />
<h3 id="heading-example-2-rest-in-object-destructuring">Example 2: Rest in Object Destructuring</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> person = { <span class="hljs-attr">name</span>: <span class="hljs-string">"John"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span>, <span class="hljs-attr">city</span>: <span class="hljs-string">"New York"</span> };
<span class="hljs-keyword">const</span> { name, ...rest } = person;

<span class="hljs-built_in">console</span>.log(name); <span class="hljs-comment">// Output: John</span>
<span class="hljs-built_in">console</span>.log(rest); <span class="hljs-comment">// Output: { age: 25, city: 'New York' }</span>
</code></pre>
<p>The <code>...rest</code> collects the remaining properties (<code>age</code> and <code>city</code>) into an object.</p>
<hr />
<h2 id="heading-what-is-the-spread-operator-spread">What is the Spread Operator (<code>...spread</code>)?</h2>
<p>The <strong>spread operator (</strong><code>...</code>) expands elements of an <strong>array, object, or iterable</strong> into individual values.</p>
<h3 id="heading-example-1-copying-an-array">Example 1: Copying an Array</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> arr1 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];
<span class="hljs-keyword">const</span> arr2 = [...arr1, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>];

<span class="hljs-built_in">console</span>.log(arr2); <span class="hljs-comment">// Output: [1, 2, 3, 4, 5, 6]</span>
</code></pre>
<p>Here, <code>arr1</code> is expanded inside <code>arr2</code>, effectively copying its elements.</p>
<hr />
<h3 id="heading-example-2-merging-objects">Example 2: Merging Objects</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> obj1 = { <span class="hljs-attr">a</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> };
<span class="hljs-keyword">const</span> obj2 = { <span class="hljs-attr">c</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">d</span>: <span class="hljs-number">4</span> };

<span class="hljs-keyword">const</span> mergedObj = { ...obj1, ...obj2 };
<span class="hljs-built_in">console</span>.log(mergedObj); <span class="hljs-comment">// Output: { a: 1, b: 2, c: 3, d: 4 }</span>
</code></pre>
<p>The spread operator merges both objects into a new one.</p>
<hr />
<h2 id="heading-shallow-copy-vs-deep-copy">Shallow Copy vs. Deep Copy</h2>
<p>When working with objects, understanding <strong>shallow copy</strong> and <strong>deep copy</strong> is crucial.</p>
<h3 id="heading-shallow-copy-one-level-copy">Shallow Copy (One-Level Copy)</h3>
<p>A <strong>shallow copy</strong> copies only the first level of an object. If the object contains nested structures, changes to the nested values will reflect in the original object.</p>
<h4 id="heading-example-shallow-copy-using-spread-operator">Example: Shallow Copy Using Spread Operator</h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> obj1 = { <span class="hljs-attr">a</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">b</span>: { <span class="hljs-attr">c</span>: <span class="hljs-number">2</span> } };
<span class="hljs-keyword">const</span> obj2 = { ...obj1 };

obj2.b.c = <span class="hljs-number">99</span>;

<span class="hljs-built_in">console</span>.log(obj1.b.c); <span class="hljs-comment">// Output: 99 (changes reflected)</span>
<span class="hljs-built_in">console</span>.log(obj2.b.c); <span class="hljs-comment">// Output: 99</span>
</code></pre>
<p>Why? The spread operator only copies the outer object. The <code>b</code> property is still <strong>referencing</strong> the original nested object.</p>
<hr />
<h3 id="heading-deep-copy-fully-cloned">Deep Copy (Fully Cloned)</h3>
<p>A <strong>deep copy</strong> creates a completely independent object. Changes in the copy do not affect the original.</p>
<h4 id="heading-method-1-using-jsonparsejsonstringifyobj">Method 1: Using <code>JSON.parse(JSON.stringify(obj))</code></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> obj1 = { <span class="hljs-attr">a</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">b</span>: { <span class="hljs-attr">c</span>: <span class="hljs-number">2</span> } };
<span class="hljs-keyword">const</span> obj2 = <span class="hljs-built_in">JSON</span>.parse(<span class="hljs-built_in">JSON</span>.stringify(obj1));

obj2.b.c = <span class="hljs-number">99</span>;

<span class="hljs-built_in">console</span>.log(obj1.b.c); <span class="hljs-comment">// Output: 2 (original remains unchanged)</span>
<span class="hljs-built_in">console</span>.log(obj2.b.c); <span class="hljs-comment">// Output: 99</span>
</code></pre>
<p>Downside: This method does <strong>not</strong> work if the object contains functions or <code>undefined</code> values.</p>
<hr />
<h4 id="heading-method-2-using-lodashs-clonedeep">Method 2: Using Lodash’s <code>cloneDeep()</code></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> _ = <span class="hljs-built_in">require</span>(<span class="hljs-string">'lodash'</span>);

<span class="hljs-keyword">const</span> obj1 = { <span class="hljs-attr">a</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">b</span>: { <span class="hljs-attr">c</span>: <span class="hljs-number">2</span> } };
<span class="hljs-keyword">const</span> obj2 = _.cloneDeep(obj1);

obj2.b.c = <span class="hljs-number">99</span>;

<span class="hljs-built_in">console</span>.log(obj1.b.c); <span class="hljs-comment">// Output: 2 (original unchanged)</span>
<span class="hljs-built_in">console</span>.log(obj2.b.c); <span class="hljs-comment">// Output: 99</span>
</code></pre>
<p>Lodash provides a powerful <code>_.cloneDeep()</code> function for deep copying.</p>
<hr />
<h2 id="heading-key-differences-rest-vs-spread-shallow-vs-deep-copy">Key Differences: Rest vs. Spread, Shallow vs. Deep Copy</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>Rest (<code>...rest</code>)</td><td>Spread (<code>...spread</code>)</td><td>Shallow Copy</td><td>Deep Copy</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Purpose</strong></td><td>Collects values into an array</td><td>Expands values into individual elements</td><td>Copies first level only</td><td>Copies everything</td></tr>
<tr>
<td><strong>Use Case</strong></td><td>Function parameters, destructuring</td><td>Function arguments, copying, merging</td><td>Nested objects remain linked</td><td>Completely independent copy</td></tr>
<tr>
<td><strong>Changes Original?</strong></td><td>No</td><td>No</td><td>Yes (nested values)</td><td>No</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-conclusion">Conclusion</h2>
<ul>
<li><p>The <strong>rest operator (</strong><code>...rest</code>) gathers multiple elements into an array.</p>
</li>
<li><p>The <strong>spread operator (</strong><code>...spread</code>) expands elements into individual values.</p>
</li>
<li><p><strong>Shallow copy (</strong><code>...spread</code>) only copies the first level, keeping nested references intact.</p>
</li>
<li><p><strong>Deep copy (</strong><code>JSON.parse()</code>, <code>_.cloneDeep()</code>) ensures full independence of objects.</p>
</li>
</ul>
<p>Now that you know these concepts, go ahead and use them in your JavaScript projects.</p>
<p>Did you find this helpful? Share your thoughts in the comments.</p>
]]></content:encoded></item></channel></rss>