Why does this object update affect both variables?

Consider this snippet.

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

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

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

WaffleFries

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

Yoshiii