<?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: Salman Sadi</title>
    <description>The latest articles on DEV Community by Salman Sadi (@salman165324).</description>
    <link>https://dev.to/salman165324</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%2F1263871%2F716a083d-b402-4c6b-807d-820a37db48ae.png</url>
      <title>DEV Community: Salman Sadi</title>
      <link>https://dev.to/salman165324</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/salman165324"/>
    <language>en</language>
    <item>
      <title>Why Mutating an Object Returned by Array.find() Changes the Original Array</title>
      <dc:creator>Salman Sadi</dc:creator>
      <pubDate>Thu, 25 Dec 2025 09:28:52 +0000</pubDate>
      <link>https://dev.to/salman165324/why-mutating-an-object-returned-by-arrayfind-changes-the-original-array-2gj4</link>
      <guid>https://dev.to/salman165324/why-mutating-an-object-returned-by-arrayfind-changes-the-original-array-2gj4</guid>
      <description>&lt;p&gt;JavaScript objects don’t behave the way many people expect. Can you guess what the output of the following code?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const users = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" }
];

const foundUser = users.find(u =&amp;gt; u.id === 1);

foundUser.name = "Updated John";

console.log(users);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✔ The output of the code above will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
 { id: 1, name: "Updated John" },
 { id: 2, name: "Jane" }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This surprises many people, but it is completely expected behavior in JavaScript. &lt;/p&gt;

&lt;p&gt;🤔Why this happens? &lt;br&gt;
→ Arrays in JavaScript store references to objects&lt;br&gt;
→ Array.find() returns the actual object, not a copy&lt;br&gt;
→ Objects are reference types, not value types &lt;/p&gt;

&lt;p&gt;So after this line:&lt;br&gt;
&lt;code&gt;const foundUser = users.find(u =&amp;gt; u.id === 1);&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
👉 Both of these point to the same object in memory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;users[0] ────┐
 ├──► { id: 1, name: "John" }
foundUser ───┘ 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 When you do:&lt;br&gt;
&lt;code&gt;foundUser.name = "Updated John";&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
You are mutating that shared object.&lt;br&gt;
 Since the array holds a reference to the same object, the array reflects the change.&lt;/p&gt;

&lt;p&gt;💡 A safer approach is to update immutably (create a new array and a new object):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const updatedUsers = [];

const updatedUsers = users.map(user =&amp;gt;
 user.id === 1 ? { ...user, name: "Updated John" } : user
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;▶ But remember: &lt;code&gt;{ ...user }&lt;/code&gt; is a shallow copy. If user contains nested objects, those nested references are still shared. In that case, you must copy the nested structure you modify, or use a deep clone.&lt;/p&gt;

&lt;p&gt;▶ There is another option which is called &lt;code&gt;structuredClone().&lt;/code&gt;&lt;br&gt;
This function returns a deep copy of the object you are passing as an argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const copy = structuredClone(user);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now mutating copy won’t affect the original object. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
