wiki/content/20200826201737-mutability.md

36 lines
732 B
Markdown
Raw Permalink Normal View History

2024-05-06 20:40:05 +00:00
---
2024-10-30 17:34:11 +00:00
date: 2020-08-26
2024-05-06 20:40:05 +00:00
id: fd131977-33f2-42cd-9a3b-46ffd58b9e43
title: 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
``` javascript
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
``` javascript
const score = { visitors: 0, home: 1 };
score.visitors = 23;
console.log(score); // { visitors: 23, home: 1 }
```