01 / 04
If you can take it out, you can put it back
Loops, and where your data lives
INPUT · Slides
01 / 04
Since pet.age got the value out, putting that same writing on the left lets you change it.
The shape is object.name = new value;. The same thinking as changing an array by number.
const pet = { name: "Mofu", age: 3 };pet.age = 4;console.log(pet.age);Result
4
02 / 04
+= and ++ work on properties as well, so "add a year" or "take three off the stock" comes out short.
As with arrays, even an object made with const lets you change the contents. So objects are normally made with const too.
const stock = { name: "seaweed", count: 10 };stock.count -= 3;console.log(stock.count);Result
7
03 / 04
Assign to a name that is not there yet and that property gets created.
So you can build one with a little information and add to it as you go.
const user = { name: "Yui" };user.age = 12;console.log(user.name);console.log(user.age);Result
Yui 12
04 / 04
Here is the one to watch. After catching it as let p = item.price;, changing p does not change the object.
What you were handed is a copy of the value. If you want to change the object, write item.price = ....
const item = { price: 180 };let p = item.price;p += 100;console.log(p);console.log(item.price);Result
280 180