DEV Community

Discussion on: Basics of javascript objects

Collapse
 
pentacular profile image
pentacular

Data stored by value v/s stored by reference

This section is generally incorrect.

There is no difference between object values and other values regarding value/reference semantics or mutability.

Consider that the following example will result in b.name === 'a'.
If b were a reference to a, then it would result in b.name === 'b'.

let a = { name: 'a' };
let b = a;
a = { name: 'b' };

The key here is that only variables and properties are mutable.

So when you say b = a; you are copying the immutable value of a.

When you say a.name = 'd'; you are modifying a property associated with a, but you are not changing the value of a.

I keep seeing people making similar claims to this, so I'm curious where they're getting this misinformation -- could you let me know?