Code / JavaScript from Zero / Unit 4: Objects and functions

Objects ऑब्जेक्ट

Lesson 8 of 15 · about 10 minutes · Group named values: a product or a student.

⚡ 0 XP

Step 1 of 1

Keys and values

name"Asha"class9city"Surat"

An object groups related values under names (called keys):

const student = { name: "Asha", class: 9, city: "Surat" };

  • Read a value with a dot: student.name is "Asha"
  • Change a value: student.city = "Vadodara";
  • Add a new key: student.house = "Green";

Even with const you can change what is inside the object — you just can’t replace the whole object.

Example
const student = { name: "Asha", class: 9, city: "Surat" };
student.city = "Vadodara";
student.house = "Green";
console.log(`${student.name} lives in ${student.city}`);

🎯 Challenge: A kirana product +15 XP

Create product with name "Parle-G", price 10 and inStock true. The price went up: change price to 12. Add a new key category with "Biscuits". Print Parle-G costs ₹12 using the object’s values.

Try twice to unlock the solution (0/2)

script.jsCtrl ⏎
Console

Output from console.log appears here.

Checklist

Press Run to check your code.

  • product has name “Parle-G” and inStock true
  • price is now 12
  • category is “Biscuits”
  • The console shows “Parle-G costs ₹12”
Lesson notes (text version)

1. Keys and values

An object groups related values under names (called keys):

const student = { name: "Asha", class: 9, city: "Surat" };

  • Read a value with a dot: student.name is "Asha"
  • Change a value: student.city = "Vadodara";
  • Add a new key: student.house = "Green";

Even with const you can change what is inside the object — you just can’t replace the whole object.

                const student = { name: "Asha", class: 9, city: "Surat" };
student.city = "Vadodara";
student.house = "Green";
console.log(`${student.name} lives in ${student.city}`);
              

Challenge: A kirana product

Create product with name "Parle-G", price 10 and inStock true. The price went up: change price to 12. Add a new key category with "Biscuits". Print Parle-G costs ₹12 using the object’s values.

Search by task, format or tool name.