wiki/content/20200826201737-mutability.md

732 B

date id title
2020-08-26 fd131977-33f2-42cd-9a3b-46ffd58b9e43 JavaScript Object Mutability

Description

With objects, there is a difference between having two references to the same object and having two different objects that contain the same properties.

Examples

let object1 = {value: 10};
let object2 = object1;
let object3 = {value: 10};

console.log(object1 == object2); // true
console.log(object1 == object3); // false

object1.value = 15;
console.log(object2.value); // 15
console.log(object3.value); // 10

`const` objects can have their values changed

const score = { visitors: 0, home: 1 };
score.visitors = 23;

console.log(score); // { visitors: 23, home: 1 }