TalentJR

🟨 JavaScript tutorial for beginners · Class 8 · Lesson 10 of 18 · Free

Objects Basics in JavaScript – Keep Related Data Together

⏱ About 35 minutes · ▶ run real JavaScript here — nothing to install · no sign-in

In this lesson you will

  • Create an object and read its properties with dot and bracket notation
  • Add, change and delete properties, and write a method that uses this
  • Loop over an object and over an array of objects

Your school ID card has many details about ONE student: name, class, roll number, blood group. Keeping them in four separate variables gets messy fast. A JavaScript object is like that ID card: one thing with named parts.

What is an object in JavaScript?

An object groups related values under names. You write it with curly braces { }. Each part is a key: value pair, and pairs are separated by commas.

The key is the name (like name or rollNo). The value can be anything: a string, a number, true/false, even an array.

An array is good for a LIST of similar things. An object is good for ONE thing with many details.

🎬 How JavaScript thinks — step 1 of 5JavaScript is about to run line 1
▶const student = {
2 name: "Asha",
3 class: 8,
4 rollNo: 17,
5 subjects: ["Maths", "Science"],
6 hasBus: true
7};
8console.log(student);
9console.log(student.name);
10console.log(student.subjects[1]);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
{ name: "Asha", class: 8, rollNo: 17, subjects: ["Maths", "Science"], hasBus: true }
Asha
Science

How do I read a property: dot or square brackets?

Dot notation is the usual way: student.name. It is short and easy to read.

Bracket notation uses a string: student["name"]. You need it when the key is stored in a variable, or has a space or dash in it.

A key that does not exist gives undefined, just like a missing array index.

🟨 JavaScript — you can edit this code
Console output
92
85
78
92
undefined

💡 Try it: change subject to "science". The same line now reads a different property.

How do I add, change or delete properties in a JavaScript object?

To change a value, assign to it: player.runs = 50. To add a new property, assign to a key that is not there yet — JavaScript creates it.

delete removes a property completely. To check if a key is there, ask "runs" in player — it answers true or false.

The object is const, yet we can change its insides — the same rule you saw for arrays in Lesson 7.

🎬 How JavaScript thinks — step 1 of 9JavaScript is about to run line 1
▶const player = { name: "Virat", runs: 45 };
2player.runs = player.runs + 6;
3player.balls = 30;
4player.isOut = false;
5console.log(player);
6delete player.isOut;
7console.log(player);
8console.log("runs" in player, "isOut" in player);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
{ name: "Virat", runs: 51, balls: 30, isOut: false }
{ name: "Virat", runs: 51, balls: 30 }
true false

What is a method, and what does this mean in JavaScript?

A method is a function stored inside an object. It describes something the object can DO.

Inside a method, this means "the object I belong to". So this.balance reads this wallet's own balance.

Call a method with brackets: wallet.spend(30). Watch the balance change in the trace.

🎬 How JavaScript thinks — step 1 of 9JavaScript is about to run line 1
▶const wallet = {
2 owner: "Ravi",
3 balance: 200,
4 spend(amount) {
5 this.balance = this.balance - amount;
6 return `${this.owner} spent ₹${amount}. Left: ₹${this.balance}`;
7 }
8};
9console.log(wallet.spend(30));
10console.log(wallet.spend(50));
11console.log(wallet.balance);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
Ravi spent ₹30. Left: ₹170
Ravi spent ₹50. Left: ₹120
120

💡 spend(amount) { … } is the short way to write spend: function (amount) { … }.

How do I loop through an object or an array of objects?

Object.keys(obj) gives an array of the keys. Object.entries(obj) gives [key, value] pairs. for…in also visits every key.

Most real data is an array of objects: a class list, a cricket team. You can use map and filter from Lesson 8 on it.

Here we find students with 80 or more in maths.

🟨 JavaScript — you can edit this code
Console output
["maths", "science", "english"]
maths → 92
science → 85
english → 74
80+ in maths: ["Asha", "Meera"]

How do I show a JavaScript object on a web page?

Objects are perfect for cards on a page: one object, many fields, each shown in its place.

The button changes a property, then draws the card again. Click "Next class" a few times.

🟨 JavaScript — you can edit this code
📄 HTML of the page (tap to see / edit)
🖥️ Your web page — try clicking it
Console output
{ name: "Meera", class: 8, house: "Blue", rollNo: 21 }

💡 const [key, value] takes the two parts of each pair into two variables at once.

🤔 Predict the output

Think like JavaScript: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

const p = { name: "Ravi", runs: 45 };
p.runs = p.runs + 5;
console.log(p.runs);

Question 2. What does this print?

const book = { title: "Atlas", pages: 120 };
console.log(book.author);

Question 3. What does this print?

const key = "city";
const trip = { city: "Pune", days: 3 };
console.log(trip.key, trip[key]);

🧩 Your challenge

Make a train ticket object with from: "Mumbai", to: "Pune", passengers: 3 and fare: 250 (per person). Add a method total() that returns passengers × fare. Then add a new property coach: "S4". Print the summary. Expected output: Mumbai → Pune, coach S4 Passengers: 3 Total fare: ₹750

Expected output in the console:
Mumbai → Pune, coach S4
Passengers: 3
Total fare: ₹750
🟨 JavaScript — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

const student = { name = "Asha", class = 8 };
console.log(student);

SyntaxError: Invalid shorthand property initializer

✅ Right

const student = { name: "Asha", class: 8 };
console.log(student);

Inside an object, use a colon between key and value: name: "Asha". The = sign is for variables.

❌ Wrong

const student = { name: "Asha" };
console.log(student.address.city);

TypeError: Cannot read properties of undefined (reading 'city')

✅ Right

const student = { name: "Asha", address: { city: "Nagpur" } };
console.log(student.address.city);

student.address is undefined, and you cannot read .city from undefined. Make sure the inner object exists first.

❌ Wrong

const marks = { maths: 92, science: 85 };
const subject = "maths";
console.log(marks.subject);

No error — but it prints undefined, because it looks for a property called "subject".

✅ Right

const marks = { maths: 92, science: 85 };
const subject = "maths";
console.log(marks[subject]);

The dot always uses the exact word after it. To use a key stored in a variable, use square brackets.

📌 Remember

  • An object stores key: value pairs inside { }, separated by commas.
  • Read with obj.key, or obj["key"] / obj[variable] for keys in variables.
  • Assign to change or add a property; delete removes one.
  • A method is a function in an object; this means the object itself.
  • Object.keys / Object.entries / for…in loop over an object.
  • An array of objects + filter and map = real-world data.

Finished the quiz and the challenge?

Questions students ask

What is the difference between an array and an object in JavaScript?

An array is an ordered list, read by number: arr[0]. An object is a set of named details, read by key: obj.name. Use an array for many similar things and an object for one thing with many details.

What does this mean in a JavaScript object?

Inside a method, this is the object the method was called on. In wallet.spend(30), this is wallet, so this.balance is wallet's balance.

Can I change a const object?

Yes, you can change, add and delete its properties. What you cannot do is make the const variable point to a completely new object.

Is a JavaScript object the same as JSON?

They look similar, but JSON is text used to send data. In JSON, keys must be in double quotes and there are no functions. You will learn JSON in Class 9.