Aral Code

Arrays & Objects Lesson 20 of 30

JavaScript Objects

Group related data into objects with properties and methods — read, change, add and delete properties, loop through them, and store a whole class as an array of objects.

An array is a list of values. But a student isn't just a list: a student has a name, a grade level, a section and an average. An object groups related information under named labels, like a filled-out form.

Creating an object

const student = {
  firstName: "Maria",
  lastName: "Clara",
  grade: 10,
  section: "Rizal",
  isOfficer: true,
};

Each line is a property: a name (the key), a colon, and a value. Properties are separated by commas.

Reading properties

There are two ways: dot notation and bracket notation.

Example Reading object properties
<pre id="out"></pre>
<script>
  const student = { firstName: "Maria", lastName: "Clara", grade: 10, section: "Rizal" };
  const key = "section";
  document.getElementById("out").textContent =
    "Dot: " + student.firstName + "\n" +
    "Bracket: " + student["lastName"] + "\n" +
    "Using a variable: " + student[key] + "\n" +
    "Missing: " + student.age;
</script>

Use dot notation normally. Use brackets when the property name is stored in a variable, or contains spaces or dashes.

Changing, adding and deleting

student.section = "Bonifacio";   // change
student.average = 94.5;          // add a new property
delete student.isOfficer;        // remove a property

Methods: functions inside objects

When a property holds a function, it's called a method. Inside a method, this refers to the object itself:

Example An object with a method
<p id="out"></p>
<script>
  const student = {
    firstName: "Jose",
    lastName: "Rizal",
    fullName() {
      return `${this.firstName} ${this.lastName}`;
    },
  };
  document.getElementById("out").textContent = "Full name: " + student.fullName();
</script>

You've been using methods all along: console.log is the log method of the console object, and Math.round is a method of Math.

Looping through an object

Example for...in and Object.entries
<pre id="out"></pre>
<script>
  const prices = { pandesal: 25, kape: 8, itlog: 9, noodles: 15 };
  let log = "";
  for (const [item, price] of Object.entries(prices)) {
    log += `${item}: ₱${price}\n`;
  }
  log += "Items: " + Object.keys(prices).join(", ");
  document.getElementById("out").textContent = log;
</script>
  • Object.keys(obj): array of property names
  • Object.values(obj): array of values
  • Object.entries(obj): array of [key, value] pairs

Arrays of objects

This is how real apps store data: a list of records. Each object is one row, like a row in a spreadsheet.

Example A class record
<table id="t" border="1" cellpadding="6" style="border-collapse:collapse"></table>
<p id="best"></p>
<script>
  const students = [
    { name: "Ana", math: 88, science: 91 },
    { name: "Ben", math: 76, science: 82 },
    { name: "Carla", math: 95, science: 93 },
  ];

  let rows = "<tr><th>Name</th><th>Math</th><th>Science</th><th>Average</th></tr>";
  for (const s of students) {
    s.average = (s.math + s.science) / 2;
    rows += `<tr><td>${s.name}</td><td>${s.math}</td><td>${s.science}</td><td>${s.average}</td></tr>`;
  }
  document.getElementById("t").innerHTML = rows;

  const best = students.reduce((a, b) => (b.average > a.average ? b : a));
  document.getElementById("best").textContent = `Top student: ${best.name} 🏆`;
</script>

Shorthand properties

When a variable has the same name as the property, you can write it once:

const name = "Ana";
const grade = 9;
const s = { name, grade };   // same as { name: name, grade: grade }

Destructuring

Destructuring pulls properties out into variables in one line:

const { firstName, section } = student;
// same as:
// const firstName = student.firstName;
// const section = student.section;

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. How do you read the grade property of student?
2. What is a method?
3. What does Object.keys({a: 1, b: 2}) return?
4. Inside a method, what does this usually refer to?

By Sir Reyel · Last updated 2026-12-04