Module 21: Arrays and Objects

Lesson 3 of 5 · 15–25 minutes

Objects

🏠The everyday picture

A product's label in a shop: name, price, size, stock. Each detail has a heading and a value. An object is that label in code.

📘What it really is

const product = {
  id: "P001",
  name: "Notebook",
  price: 60,
  inStock: true,
};

🖱️Try it

Create an object for one product, with id, name, price, category and stock. Print each property using Object.entries and a for...of loop:

for (const [key, value] of Object.entries(product)) {
  console.log(`${key}: ${value}`);
}

Then make a copy with a new price and print both.

✅Quick check

You need a property whose name is stored in a variable field. How do you read it?

Show the answer

product[field]. The dot form would look for a property literally called "field".

const b = { ...a, price: 70 }. Does a.price change?

Show the answer

No. b is a new object; a is untouched.

← PreviousArray operations
ProLearners Assist