Lesson 6 of 10

Working With Data

Lesson 6: Arrays and Objects

Arrays store lists. Objects store details. Most real projects use both together.

Learning Goals

Real Example: Restaurant Menu

Imagine you are running a small restaurant. You want to keep a list of what you sell. In JavaScript, you would write:

// This is a list (array) of menu items
let menuItems = ["Rolex", "Matoke", "Tilapia"];

Now, let's say a customer makes an order. We want to store the details of that order. For that, we use an object:

// This is an object with details about the order
let order = {
  item: "Rolex",
  quantity: 2,
  paid: false
};

When we write let order = { item: "Rolex", quantity: 2, paid: false };, we mean: we are creating one object named order with related details inside it.

Simple meaning:

We can change the order, for example, when the customer pays:

order.paid = true;

And we can add new items to our menu:

menuItems.push("Passion Juice");

Let's see what happens if we print these out:

console.log(menuItems[0]); // Rolex
console.log(order.item);   // Rolex
console.log(order);        // Shows all order details
Tip: Run node lesson6.js in your terminal to see the output. Try changing the values and see what happens.

Real Example 2: Student Record

let students = ["Asha", "Brian", "Cynthia"];

let studentRecord = {
  name: "Asha",
  score: 75,
  passed: true
};

console.log(students[1]);
console.log(studentRecord.score);

When we write this, we mean: one structure keeps many names, another keeps one student's details.

Deeper Explanation

Data structures are not advanced extras. They are the center of real software. Most applications are simply reading, updating, and displaying structured data. Arrays help when order matters or when you have many items. Objects help when one item has many properties that belong together.

A strong beginner habit is to pause before coding and ask: "Am I handling one thing with details, or many things in a list?" That single question helps you choose between object and array quickly and keeps your code logical.

Common Beginner Mistakes

  1. Using { } when you needed a list of many items.
  2. Trying to access array values with dot notation instead of index.
  3. Misspelling object keys like quantitiy instead of quantity.

Practice

  1. Create an array with 4 fruits.
  2. Create an object with your profile data.
  3. Print one array item and one object value.
  4. Update one value in your object and print it again.

Homework

Create an array of 3 students. Create one object for each student with name and score. Then update one score and print before and after.

Answer Guide
let students = [
  { name: "Asha", score: 75 },
  { name: "Brian", score: 62 },
  { name: "Cynthia", score: 84 }
];

console.log(students[0].name);
console.log(students[2].score);

students[1].score = 70;
console.log(students[1].score);
← Previous Lesson Next Lesson →