Learning Goals
- Create arrays and objects.
- Read values from arrays and objects.
- Update values in arrays and objects.
- Explain in plain words what brackets and braces mean.
Working With Data
Arrays store lists. Objects store details. Most real projects use both together.
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:
[ ] means list of items (array).{ } means one item with named details (object).order.item means get the item value from the order object.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
node lesson6.js in your terminal to see the output. Try changing the values and see what happens.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.
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.
{ } when you needed a list of many items.quantitiy instead of quantity.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.
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);