edubun
English
1 / 13

11 / 12 · Intro

Objects

An object groups related values under names. In { name: "Coco", age: 3 }, pet.name gives "Coco" and pet.age gives 3.

Jump to the 10 practice questions

You will learn

  • Group values under names in an object and read them
  • Change and add properties, and use arrays of objects
  • Call a method that uses this

Before this: Functions

1Explain

Values with labels

An object is written in curly braces, with each property as key: value and commas between them. A trailing comma after the last property is allowed. To read a property, write the object, a dot and the key: pet.name. The other form uses square brackets and a string, pet["kind"]. Brackets are needed when the key is stored in a variable, as in pet[field], or when it contains a space.

The object is drawn as key → value rows. pet.name and pet["kind"] read two of them.

  1. const pet = {
  2. name: "Coco",
  3. kind: "cat",
  4. age: 3,
  5. };
  6. console.log(pet.name); // Coco
  7. console.log(pet["kind"]); // cat
  8. console.log(pet.age + 1); // 4
Variables
Output
Coco
cat
4

2Explore

Read a property

A missing property reads as undefined. The whole object prints in the compact form used on this site.

expression
const pet = { name: "Coco", kind: "cat", age: 3 };
console.log(pet.name);
Output
Coco

3Problem

Bracket notation

What does this print?
const car = { brand: "Kia", year: 2020 };
console.log(car["brand"]);

Hint

car["brand"] reads the same property as car.brand.

Solution

Answer: Kia

Bracket notation with the string "brand" reads the same property as car.brand.

4Problem

A property that is not there

What does this print?

const car = { brand: "Kia" };
console.log(typeof car.color);

Hint

Reading a missing property does not stop the program.

Solution

Answer: undefined

A missing property reads as undefined, so its type is "undefined".

5Explain

Changing and adding properties

Assign to a property to change it, and assign to a new key to add one. As with arrays, this works even when the object was declared with const, because the variable still points to the same object.

Watch the rows: += changes score, a new key adds level, and delete removes it again.

  1. const player = { name: "Sam", score: 0 };
  2. player.score += 10;
  3. player.level = 2;
  4. console.log(`${player.name} ${player.score} ${player.level}`); // Sam 10 2
  5. delete player.level;
  6. console.log(player.level); // undefined
Variables
Output
Sam 10 2
undefined

6Problem

How many keys?

What does this print?

const p = { name: "Lee" };
p.age = 40;
console.log(Object.keys(p).length);

Hint

name was there, and age was added.

Solution

Answer: 2

Adding age gives two keys: name and age.

7Explain

Lists of objects

Arrays and objects combine naturally. An array of objects is how most programs hold records: a shopping basket, a class list, a set of search results. You loop over the array and read the properties you need from each object.

Each round, line is the next object in the array, and line.price is added to total.

  1. const basket = [
  2. { item: "pen", price: 2 },
  3. { item: "book", price: 12 },
  4. { item: "bag", price: 25 },
  5. ];
  6. let total = 0;
  7. for (const line of basket) {
  8. total += line.price;
  9. }
  10. console.log("Total:", total); // Total: 39
Variables
Output
Total: 39

8Problem

Add the n values

What does this print?

const items = [{ n: 2 }, { n: 5 }, { n: 1 }];
let t = 0;
for (const it of items) {
  t += it.n;
}
console.log(t);

Hint

Read it.n from each object: 2, 5, 1.

Solution

Answer: 8

2 + 5 + 1 = 8.

9Explain

Methods and this

A property whose value is a function is called a method. You have been calling methods all along: console.log is the log method of the console object, and Math.round is the round method of Math. Inside a method, the keyword this refers to the object the method was called on, so a method can read its own object's properties. Finally, Object.keys(pet) gives an array of the keys, here ["name", "kind", "age"], which is useful for looping over every property.

const counter = {
  count: 0,
  add() {
    this.count++;
  },
};
counter.add();
counter.add();
console.log(counter.count);   // 2
Output
2

10Practice

Describe the pet

Use the three properties and a template literal to print Coco is a 3 year old cat.

Expected output
Coco is a 3 year old cat
Your code
const pet = { name: "Coco", kind: "cat", age: 3 };
console.log(pet.name);
Hint

Inside the template, read each property through pet: ${pet.name}.

A solution
const pet = { name: "Coco", kind: "cat", age: 3 };
console.log(`${pet.name} is a ${pet.age} year old ${pet.kind}`);

11Practice

Total of the basket

Add a loop that adds every price to total, so the program prints Total: 39.

Expected output
Total: 39
Your code
const basket = [
  { item: "pen", price: 2 },
  { item: "book", price: 12 },
  { item: "bag", price: 25 },
];
let total = 0;
// Loop over basket and add up the prices.

console.log("Total:", total);
Hint

for (const line of basket) gives each object; add line.price with +=.

A solution
const basket = [
  { item: "pen", price: 2 },
  { item: "book", price: 12 },
  { item: "bag", price: 25 },
];
let total = 0;
for (const line of basket) {
  total += line.price;
}
console.log("Total:", total);

12Explain

Common mistakes

const user = { name: "Ana" };
console.log(user.adress.city);
Error message
TypeError: Cannot read properties of undefined (reading 'city') (line 2)
  1. The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
  2. After the colon: what went wrong, usually with the name involved.
  3. The line number shows where the program stopped. A SyntaxError stops it before any line runs.

user.adress is misspelled, so it is undefined, and undefined has no city.

const pet = { name: "Coco" age: 3 };
Error message
SyntaxError: Unexpected identifier 'age'
  1. The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
  2. After the colon: what went wrong, usually with the name involved.
  3. The line number shows where the program stopped. A SyntaxError stops it before any line runs.

Properties are separated by commas. Without the comma after "Coco" the object is not valid JavaScript.

13

Summary

  • Objects hold key: value properties in { }.
  • Read with obj.key or obj["key"]; brackets for keys in variables.
  • Assign to change or add a property; missing keys read as undefined.
  • Arrays of objects hold records; loop and read each property.
  • A method is a function property; this is its object.

Next unitProject: tip calculator and FizzBuzz
Full text · The whole lesson as one text

Values with labels

An array is good for a list of similar things in order. Many things in the world are better described by a set of labelled facts: a pet has a name, a kind and an age; a product has a title, a price and a stock count. An object stores exactly that. It is a collection of properties, and each property is a key (the label) paired with a value.

const pet = {
  name: "Coco",
  kind: "cat",
  age: 3,
};
console.log(pet.name);        // Coco
console.log(pet["kind"]);     // cat
console.log(pet.age + 1);     // 4

An object is written in curly braces, with each property as key: value and commas between them. A trailing comma after the last property is allowed. To read a property, write the object, a dot and the key: pet.name. The other form uses square brackets and a string, pet["kind"]. Brackets are needed when the key is stored in a variable, as in pet[field], or when it contains a space. Reading a key that does not exist gives undefined.

Changing and adding properties

Assign to a property to change it, and assign to a new key to add one. As with arrays, this works even when the object was declared with const, because the variable still points to the same object.

const player = { name: "Sam", score: 0 };
player.score += 10;
player.level = 2;
console.log(`${player.name} ${player.score} ${player.level}`);   // Sam 10 2
delete player.level;
console.log(player.level);   // undefined

Lists of objects

Arrays and objects combine naturally. An array of objects is how most programs hold records: a shopping basket, a class list, a set of search results. You loop over the array and read the properties you need from each object.

const basket = [
  { item: "pen", price: 2 },
  { item: "book", price: 12 },
  { item: "bag", price: 25 },
];
let total = 0;
for (const line of basket) {
  total += line.price;
}
console.log("Total:", total);   // Total: 39

Values inside an object can be anything, including arrays and other objects. const user = { name: "Ana", tags: ["admin", "editor"] }; lets you write user.tags[0] to reach "admin". Read such chains from left to right: start at user, go to its tags property, then take index 0.

Methods and the keys of an object

A property whose value is a function is called a method. You have been calling methods all along: console.log is the log method of the console object, and Math.round is the round method of Math. Inside a method, the keyword this refers to the object the method was called on, so a method can read its own object's properties. Finally, Object.keys(pet) gives an array of the keys, here ["name", "kind", "age"], which is useful for looping over every property.

const counter = {
  count: 0,
  add() {
    this.count++;
  },
};
counter.add();
counter.add();
console.log(counter.count);   // 2

Worked example. A book record { title: "Notes", pages: 120, read: 45 } can report progress with Math.round(book.read / book.pages * 100), which is 38, meaning 38 percent. Naming the values pages and read makes the formula readable in a way that b[1] / b[0] never would.

Array or object?

A quick test decides which one to use. If the values are the same kind of thing and their order matters, such as the scores of a class or the steps of a recipe, use an array and reach them by position. If the values are different facts about one thing, such as the name, price and stock of a product, use an object and reach them by name. Most real data mixes the two: an array of products, where each product is an object. When you print an object in the practice box you will see it in a compact form such as {"name":"Coco","age":3}; reading single properties gives cleaner output.

Key points
  • Objects hold key: value properties in { }.
  • Read with obj.key or obj["key"]; brackets for keys in variables.
  • Assign to change or add a property; missing keys read as undefined.
  • Arrays of objects hold records; loop and read each property.
  • A method is a function property; this is its object.

Practice questions from Objects on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (2)
  • MDN Web Docs, JavaScript Guide (developer.mozilla.org/en-US/docs/Web/JavaScript/Guide, chapters Introduction, Grammar and types, Control flow, Loops and iteration, Functions, Expressions and operators, Numbers and strings, Indexed collections, Working with objects; checked 2026-10-17)
  • Ecma International, ECMA-262 ECMAScript 2026 Language Specification, 17th edition, June 2026 (tc39.es/ecma262 and ecma-international.org/publications-and-standards/standards/ecma-262, checked 2026-10-17).

Checked: 2026-10-17