Objects, destructuring and spread
Objects hold most of the data in a real app: orders, users, configs. This note covers reading objects, destructuring (object, nested, rename, default, array), the three dots as spread and rest, and the difference between a shallow and a deep copy.
8.1 Quick refresher: objects
- An object is a collection of key-value pairs (name, price, inStock).
- Read with a dot (product.name) or square brackets (product[key]) when the key is in a variable.
- Object.keys gives all keys, Object.values gives all values.
JavaScript
const product = { name: "Shoes", price: 1999, inStock: true,}; console.log(product.name);const key = "price";console.log(product[key]); console.log(Object.keys(product));console.log(Object.values(product));Output
> Shoes> 1999> [ 'name', 'price', 'inStock' ]> [ 'Shoes', 1999, true ]8.2 Object destructuring
- Without it you write order.id, order.customer, order.total again and again.
- const { id, customer, total } = order pulls the keys into variables with the same names.
- Nested: const { address: { city } } = order goes inside address.
- Rename: total: paid puts the value of total into paid.
- Default: coupon = "NIL" is used when the key is missing (instead of undefined).
JavaScript
const order = { id: "OD1023", customer: "Riya", total: 1499, address: { city: "Pune", pin: "411001" },}; const { id, customer, total } = order;const { address: { city } } = order;console.log(id, customer, total, city); const { total: paid, coupon = "NIL" } = order;console.log(paid, coupon);Output
> OD1023 Riya 1499 Pune> 1499 NIL8.3 Array destructuring
- Values come by position, not by name: the first goes to gold, the second to silver.
- Swap two variables in one line: [a, b] = [b, a].
- React's useState returns [value, setter] with this same pattern.
JavaScript
const toppers = ["Riya", "Aman", "Kabir"];const [gold, silver] = toppers;console.log(gold, silver); let a = 1;let b = 2;[a, b] = [b, a];console.log(a, b);Output
> Riya Aman> 2 18.4 Spread: copy and merge
- { ...user, city: "Mumbai" } spreads all keys into a new object; the later city overrides the old one.
- The original user still has Delhi, because a new object was created.
- Arrays too: [...veg, "chicken"] copies the list and adds an item.
JavaScript
const user = { name: "Aman", city: "Delhi" };const updated = { ...user, city: "Mumbai" }; console.log(user.city);console.log(updated); const veg = ["paneer", "dal"];const menu = [...veg, "chicken"];console.log(menu);Output
> Delhi> { name: 'Aman', city: 'Mumbai' }> [ 'paneer', 'dal', 'chicken' ]8.5 Rest: collect the remaining keys
- const { password, ...safeUser } = account: password goes to its own variable, every other key is collected into safeUser.
- Useful for sending data to the frontend without the password.
- Rule: on the right side ... spreads values; on the left side ... collects them.
JavaScript
const account = { name: "Aman", email: "aman@mail.com", password: "secret123",}; const { password, ...safeUser } = account;console.log(safeUser);Output
> { name: 'Aman', email: 'aman@mail.com' }| Point | Spread | Rest |
|---|---|---|
| Where | Right side of = / in a call / literal | Left side of = / function parameters |
| Job | Expands values out | Collects remaining values |
| Object example | { ...user, city: "Mumbai" } | const { password, ...safeUser } = account |
| Array example | [...veg, "chicken"] | const [first, ...others] = list |
8.6 Checkpoint: shallow copy
- Copy order with spread, change id and address.city in the copy.
- The original id stays OD1023, but the original city becomes Delhi.
- Spread copies only top-level keys; the nested address object is shared by reference.
JavaScript
const order = { id: "OD1023", address: { city: "Pune" },}; const copy = { ...order };copy.id = "OD2048";copy.address.city = "Delhi"; console.log(order.id);console.log(order.address.city);Output
> OD1023> Delhi- Step 1
1.copy = { ...order }
- Step 2
2.New outer object: id is copied
- Step 3
3.Nested address: only the reference is copied
- Step 4
4.copy.address.city = "Delhi"
- Step 5
5.order.address.city is Delhi too
Fix: structuredClone makes new objects at every level
8.7 Deep copy with structuredClone
- A deep copy is fully independent, including nested data.
- structuredClone (built in) creates new objects at every level: the original city stays Pune.
- Special objects like Date stay a Date.
- Functions cannot be cloned.
JavaScript
const order = { id: "OD1023", address: { city: "Pune" }, placedAt: new Date("2026-01-15"),}; const copy = structuredClone(order);copy.address.city = "Delhi"; console.log(order.address.city);console.log(copy.placedAt instanceof Date);Output
> Pune> true| Method | Copy type | Nested data | Date / functions |
|---|---|---|---|
| = (assignment) | No copy, same reference | Shared | Shared |
| { ...obj } / Object.assign | Shallow | Shared by reference | Date shared |
| JSON.parse(JSON.stringify()) | Deep (old trick) | Copied | Date becomes string, functions and undefined dropped |
| structuredClone | Deep | Copied | Date kept; functions cannot be cloned |
8.8 Common mistakes
- Deep copy with JSON.stringify / parse: turns a Date into a string and silently drops functions and undefined.
- Use structuredClone, which keeps a Date as a Date.
JavaScript
// Wrongconst order = { placedAt: new Date("2026-01-15") }; const copy = JSON.parse( JSON.stringify(order));console.log(typeof copy.placedAt);JavaScript
// Rightconst order = { placedAt: new Date("2026-01-15") }; const copy = structuredClone(order);console.log(copy.placedAt instanceof Date);- Thinking Object.freeze locks everything: top-level keys are locked, but a nested object can still change.
- freeze is shallow too; freeze every level to lock nested objects.
JavaScript
const config = Object.freeze({ theme: "dark", api: { url: "/v1" },}); console.log(Object.isFrozen(config));config.api.url = "/v2";console.log(config.api.url);Output
> true> /v28.9 Rules
- Read values with destructuring and give defaults for missing values.
- Update by creating a new object with spread, not by modifying the original (this is how React state updates work).
- For an independent copy of nested data use structuredClone.
8.10 Interview tips
- Shallow copy: copies only top-level properties; nested objects are shared by reference (spread, Object.assign).
- Deep copy: new objects at every level; in modern JavaScript use structuredClone.
- The JSON trick breaks Dates and drops functions.
- Object.freeze is also shallow.