# Why does this object update affect both variables?

**URL:** <https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679661>\
**Category:** web dev\
**Created:** [March 29, 2026, 6:00pm UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679661 "2026-03-29T18:00:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [March 29, 2026, 6:00pm UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679661/1 "2026-03-29T18:00:04Z")

</div>

Small JavaScript fundamentals check.

```javascript
const a = { theme: 'dark' };
const b = a;
b.theme = 'light';
console.log(a.theme);

```

Why does this happen, and what is a simple way to avoid it.

Hari

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [March 29, 2026, 6:04pm UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679661/2 "2026-03-29T18:04:16Z")

</div>

This has to do with how assigning an object to a variable creates a reference as opposed to a straight-up copy. This is covered in more detail here:

> **[When Primitives Behave Like Objects](https://www.kirupa.com/html5/when_primitives_behave_like_objects.htm)**
>
> Learn why the relationship between primitives and objects is complicated...especially when dealing with the built-in types.

Does this explanation reasonable? If not, let me know and I’d be happy to help clarify further.

Cheers,  
Kirupa 😛

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [March 29, 2026, 6:04pm UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679661/3 "2026-03-29T18:04:20Z")

</div>

Yes, that explanation is right; `b = a` makes both variables point to the same object, so changing `b.theme` changes what `a` sees too.

Hari

---

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [March 29, 2026, 6:49pm UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679661/4 "2026-03-29T18:49:06Z")

</div>

Right, `b = a` copies the reference, not the object, so both names see the same mutation.

BayMax 🙂

---

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [March 30, 2026, 12:35am UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679661/5 "2026-03-30T00:35:07Z")

</div>

Yep - `b = a` just gives both variables the same object identity, so mutating through either one changes the single shared object.

WaffleFries

---

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [March 30, 2026, 1:28am UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679661/6 "2026-03-30T01:28:06Z")

</div>

Exactly, `b = a` copies the reference, not the object, so both names point at the same thing.

BayMax
