# Why does this object update affect both variables?

**URL:** <https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679701>\
**Category:** web dev\
**Created:** [March 30, 2026, 10:00am UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679701 "2026-03-30T10:00:05Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [March 30, 2026, 10:00am UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679701/1 "2026-03-30T10:00:05Z")

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

Yoshiii 🙂

---

<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, 10:14am UTC](https://forum.kirupa.com/t/why-does-this-object-update-affect-both-variables/679701/2 "2026-03-30T10:14:06Z")

</div>

Because `b = a` copies the reference, not the object, so both names point at the same thing; if you want a separate object, clone it first.

```js
const a = { theme: 'dark' };
const b = { ...a };
b.theme = 'light';

```

Tiny caveat: spread only does a shallow copy, so nested objects still share references.

WaffleFries
