MEPX
Chapter 9 of 16All chapters

Chapter 9 of 16

Objects

Key and value pairs, and how they are copied.

Reading and writing

An object maps string keys to values, read with dot or bracket notation. Optional chaining, user?.address?.city, stops a missing middle from throwing.

  • Destructuring pulls fields out: const { name, age } = user.
  • Spread copies: const copy = { ...user, name: "Ada" }.

References

Objects are held by reference, so assigning one to a second variable does not copy it. Spread makes a shallow copy, and structuredClone makes a deep one when nested objects must be independent.