← Learning Hub
Intermediate

Objects, Destructuring, Spread and JSON

Modern JavaScript has short ways to take values out of objects and arrays, and to copy or merge them.

Real data is full of objects inside arrays inside objects - an order has a customer, a list of items, an address. Destructuring and spread are shortcuts for the two most common jobs: taking values out of these structures, and making updated copies without changing the original. You'll see them in almost every modern JavaScript file.

Destructuring

Example
const product = { name: "Kurta", price: 899, size: "M" };

const { name, price } = product;
console.log(name, price);        // Output: Kurta 899

const { size: chosenSize = "L" } = product;   // rename + default value
console.log(chosenSize);         // Output: M

const [first, second] = ["Delhi", "Mumbai", "Pune"];
console.log(second);             // Output: Mumbai

Spread ... and rest ...

Three dots spread an array or object out, or collect the rest of the values.

Example
const a = [1, 2];
const b = [...a, 3, 4];
console.log(b);                  // Output: [ 1, 2, 3, 4 ]

const base = { theme: "light", lang: "en" };
const mine = { ...base, theme: "dark" };     // copy and change one key
console.log(mine);               // Output: { theme: 'dark', lang: 'en' }

function total(...prices) {      // rest: all arguments in one array
  return prices.reduce((s, p) => s + p, 0);
}
console.log(total(100, 200, 50)); // Output: 350

Looping over objects

Example
const stock = { pens: 40, books: 12, bags: 5 };
for (const [item, count] of Object.entries(stock)) {
  console.log(item + ": " + count);
}
// Output:
// pens: 40
// books: 12
// bags: 5
console.log(Object.keys(stock).length);   // Output: 3

JSON

JSON is text that looks like a JavaScript object. Websites and APIs send data as JSON. JSON.stringify turns an object into text, and JSON.parse turns text back into an object.

JSON is the common language of the web. When your page asks a server for products, the answer arrives as JSON text; when you save data in localStorage or send a form to an API, you convert it to JSON. Because it looks like JavaScript objects, it's easy to read and write.

Example
const order = { id: 101, items: ["Pen", "Book"], paid: true };
const text = JSON.stringify(order);
console.log(text);               // Output: {"id":101,"items":["Pen","Book"],"paid":true}

const back = JSON.parse(text);
console.log(back.items[1]);      // Output: Book
Optional chaining ?. stops errors when something is missing: user?.address?.city gives undefined instead of crashing. ?? gives a fallback: user.name ?? "Guest".
Key points
  • Destructuring pulls values out: const { name, price } = product.
  • Spread copies and merges: [...a, ...b], { ...obj, price: 99 }.
  • Object.entries loops over keys and values.
  • JSON.stringify and JSON.parse convert to and from text.

📝 Test yourself

1. What is x after: const { x = 5 } = { x: 2 }?
2. Which line copies an object and changes one key?
3. Fill in the method that turns JSON text back into an object:
const data = JSON.(text);