Objects
You'll learn to read objects, and to see when two names point to the same object.
Watch & read
Start with the required ones. The others are there when you need them.
See it step by step
Two names, one object, and then a real copy.
Explanation
An object groups values under names, called keys: { name: "Rafi", year: 2 }. You read a value with a
dot, like user.name, and change it the same way: user.year = 3.
Here is the surprising part. When you write const b = a, you do not get a second object. You get a
second name for the same object. Change it through b, and a sees the change too, because there is
only one object.
To get a real copy, spread it into a new object: const c = { ...a }. Now c is its own object, and
changing it leaves a alone. React relies on this: you make a new object instead of changing the old one.
Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3What is
user.nameifconst user = { name: "Nusrat", year: 3 }? - 2 / 3
- 3 / 3Which line makes a separate copy of
a?