Start here.
An object groups related values under property names. const user = { name: 'Ada', age: 36 }; creates one object. The braces here describe data, while braces after if or function describe a code block. user.name reads a fixed property; user['name'] reads a key written as a string. If the key comes from a variable, brackets can use that variable. Reading a property that does not exist gives undefined.
Every object literal creates a particular object with its own identity. If const second = user; runs, second and user are two names for the same object. Changing second.name also changes what user.name reads. The assignment copied a reference to the object, not all its properties. By contrast, writing another object literal with the same contents makes a different object.
Arrays are objects organized by numbered positions. The first element is at index 0, and an array's length tells how many positions it contains. Use an array when order matters, and an object when named properties explain the pieces. Identity matters for both: two arrays with the same items are distinct if created separately. To reason about a change, follow the references and ask which names still reach the same container.
Build the model.
An object groups named properties. Two objects with identical properties are still different objects. Variables can share one object, so a change through one name is visible through the other. This is reference behavior, not a special rule for a particular property.
Arrays are objects with indexed elements and a length; functions are callable objects. Use objects to represent entities with meaningful named parts, and arrays for ordered collections. The structure you choose should make the data's role obvious to the reader.
How it works.
An object literal creates a new identity each time it is evaluated. Properties are attached to that identity and can be read with dot syntax or bracket syntax. Dot syntax uses a fixed property name; brackets evaluate a key expression, which helps when the key comes from data. Reading a missing property gives undefined, so handle absence when it matters.
Assignment to another binding copies the reference to the object, not all of its properties. That is why two names can observe the same mutation. Arrays follow the same identity rule, even though their keys are usually numeric positions. When you need a separate state snapshot, make a new container; when you intentionally share an entity, keep the shared reference and make that decision clear.
Read it line by line.
const first = { theme: 'dark' };
const second = first;
second.theme = 'light';First notice. first.theme is now 'light' because both names reach the same object.
first receives a new object with theme 'dark'. second = first gives second a reference to that same object. second.theme = 'light' changes the shared object's property. Reading first.theme afterward yields 'light'. No special synchronization occurred: both names reach the same object.
See the rule again.
const a = { id: 1 };
const b = a;
const c = { id: 1 };
console.log(a === b, a === c);The output is true false. b points to the exact object a points to. c points to a new object with the same id value. If your goal is to compare ids, write a.id === c.id and state that id is the chosen definition of sameness.
One thing to remember.
Equality of shape is different from identity of object.