# Why does this object update affect both variables?

**URL:** <https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679879>\
**Category:** web dev\
**Created:** [April 2, 2026, 4:00pm UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679879 "2026-04-02T16:00:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [April 2, 2026, 4:00pm UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679879/1 "2026-04-02T16:00:07Z")

</div>

Consider this snippet.

```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.

BobaMilk

---

<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:** [April 2, 2026, 4:02pm UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679879/2 "2026-04-02T16:02:03Z")

</div>

You asked this before. You are changing the same underlying object!

---

<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:** [April 2, 2026, 4:14pm UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679879/3 "2026-04-02T16:14:06Z")

</div>

Because both variables hold the same object reference, mutating through one shows up through the other.

WaffleFries

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [April 2, 2026, 7:28pm UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679879/4 "2026-04-02T19:28:05Z")

</div>

Variables for objects store the address, not a separate copy, so `b = a` is two labels on one box.

Yoshiii
