Phase 3: JavaScript • Lesson 12

Arrays & Objects — Storing Groups of Data

Learn to store many values in one place, and give each value a name. This is how real apps hold lists of users, products, and messages.

A note on this lesson. This lesson uses short sentences and plain words. Each sentence has one idea. This makes the ideas easy to hold in your head. Code samples stay in normal JavaScript, because code has its own fixed grammar.

1. Learning Objective

So far, you stored one value in one variable. Real apps do not work that way. Real apps store many values at once.

A list of products. A list of users. A list of chat messages. You need a way to hold groups of data.

By the end of this lesson, you will be able to:

💡 Why this matters

Open any real API response. It looks like this:

[
  { "id": 1, "name": "Altamash", "role": "Developer" },
  { "id": 2, "name": "Sara", "role": "Designer" }
]

That is an array of objects. A database table looks the same way in your code. React lists, Express API responses, and MongoDB documents all use this shape. Learn this pattern once. Use it everywhere.

2. Quick Revision

Here is a short review of terms you already know. You will need them today.

Term Meaning Example
Variable A name that holds one value. const name = "Sara";
String Text, inside quotes. "hello"
Number A digit value, no quotes. 42
Boolean Only two values exist: true or false. true

3. Arrays — An Ordered List of Values

An array is a list. It holds many values in one variable. The order stays fixed, unless you change it.

You write an array with square brackets [ ]. You separate each value with a comma.

const fruits = ["apple", "banana", "mango"];

This one line replaces three separate variables. It is shorter. It is easier to loop over. It matches how real data looks.

3.1 Each Item Has a Position

Every item in an array has a position. This position is called an index. The first index is 0, not 1. This is called zero-based indexing. (MDN: Indexed collections)

index 0"apple"
index 1"banana"
index 2"mango"

You read an item with square brackets and its index.

console.log(fruits[0]); // "apple"
console.log(fruits[2]); // "mango"
console.log(fruits[5]); // undefined — no item there
⚠️ Common Mistake

The last index is not the array length. It is the length minus one. An array with 3 items has index positions 0, 1, and 2. There is no index 3.

3.2 Array Length

Use .length to count the items in an array.

console.log(fruits.length); // 3

3.3 Add and Remove Items

Use .push() to add an item to the end. Use .pop() to remove the last item.

const fruits = ["apple", "banana"];
fruits.push("mango");   // ["apple", "banana", "mango"]
fruits.pop();            // ["apple", "banana"]

Vocabulary — Arrays

Array
An ordered list of values, written with [ ]
Element / Item
One value stored inside an array
Index
The position of an item, starting at 0
.length
A count of how many items are in the array

4. Objects — Named Values

An array uses position to find a value. Sometimes position is not clear enough. You want a name instead.

An object stores values under names. Each name is called a key. Each stored value is called that key's value. Together, one key and its value form a property. (MDN: JavaScript object basics)

You write an object with curly braces { }.

const user = {
  name: "Altamash",
  age: 25,
  isStudent: true
};

4.1 Reading a Property — Two Ways

Dot notation is the common way. You write a dot, then the key name.

console.log(user.name); // "Altamash"
console.log(user.age);  // 25

Bracket notation uses square brackets and a string. Use this when the key name is stored in a variable, or has a space in it.

console.log(user["name"]); // "Altamash"

const key = "age";
console.log(user[key]); // 25 — key name comes from a variable

4.2 Changing a Property

user.age = 26;          // update an existing property
user.city = "Mumbai";   // add a new property

4.3 Objects Inside Objects

A property's value can be another object. This is called nesting. Chain the dots to reach a deep value.

const user = {
  name: "Altamash",
  address: {
    city: "Mumbai",
    zip: "400001"
  }
};

console.log(user.address.city); // "Mumbai"

Vocabulary — Objects

Object
A set of named values, written with { }
Key
The name given to a stored value
Property
One key and its value, together
Dot notation
object.key — the common way to read a value

5. The Real-World Pattern: an Array of Objects

Most real data is a list of records. Each record has named fields. This means: an array that holds objects.

const contacts = [
  { name: "Sara",  phone: "555-0101" },
  { name: "Kabir", phone: "555-0192" },
  { name: "Meera", phone: "555-0133" }
];

Read one record by its index. Then read a field on that record with a dot.

console.log(contacts[0]);       // { name: "Sara", phone: "555-0101" }
console.log(contacts[0].name);  // "Sara"
console.log(contacts[1].phone); // "555-0192"
💡 Say the shape out loud

"An array of objects" is not just a phrase. It is the shape of almost every list you will build: a product catalog, a user list, a to-do list, a set of blog posts. Learn to spot this shape on sight.

6. Visiting Every Item — the for...of Loop

Reading contacts[0], then contacts[1], then contacts[2] by hand does not scale. A list of 1,000 contacts needs a better way.

A for...of loop visits every item in an array, one at a time. Give the current item a temporary name.

const contacts = [
  { name: "Sara",  phone: "555-0101" },
  { name: "Kabir", phone: "555-0192" }
];

for (const contact of contacts) {
  console.log(contact.name);
}
// Sara
// Kabir

contact is a name you choose. On each pass through the loop, it holds the next item in the array. (MDN: for...of)

📝 A preview, not the full picture

This is the smallest loop you need today. The next lesson covers array methods like .map(), .filter(), and .forEach(). Those methods do more work in less code, and React uses .map() constantly. For now, for...of is enough to read every item in a list.

7. Try It Yourself

Change the values below. Add a contact with .push(). Run the code again. Watch the loop pick up your new item.

Ctrl + Enter to run

        

8. Practice Questions

Answer these in your head, or write them down. Ask your teacher if any answer feels unsure.

  1. What index holds the first item in an array?
  2. Given const nums = [10, 20, 30];, what does nums[1] return?
  3. Given the same array, what does nums.length return?
  4. What method adds one item to the end of an array?
  5. What are the two ways to read a property on an object?
  6. Given const user = { name: "Sara" };, how do you add a new property called age with value 30?
  7. What do we call an array that holds objects instead of plain values?
  8. What loop visits every item in an array, one at a time?

9. Build Challenge — a Contact List

🏆 Challenge

Open a fresh JavaScript file, or use the playground above. Build this:

  1. Create an array called contacts. Add three objects. Give each object a name and an email.
  2. Use console.log to print the name of the second contact, using its index.
  3. Write a for...of loop. Print each contact as one line: "Sara — sara@example.com".
  4. Use .push() to add a fourth contact. Run your loop again. Confirm the new contact appears.

Finished the build? Type /teach and tell me what you built. I will check your array, your object shape, and your loop.

10. Interactive Self-Assessment

Concept Quiz

Fill-in-the-Blank Practice

11. Lesson Summary

Quick Revision — Copy/Paste Patterns

// Array
const list = ["a", "b", "c"];
list[0];          // "a"
list.length;      // 3
list.push("d");   // add to end
list.pop();       // remove from end

// Object
const item = { key: "value" };
item.key;         // dot notation
item["key"];      // bracket notation

// Array of objects
const rows = [{ id: 1 }, { id: 2 }];
rows[0].id;       // 1

// Loop
for (const row of rows) {
  console.log(row.id);
}

12. Primary Sources & Next Step

Read these two MDN pages this week. They are the source of truth for everything in this lesson:

Next lesson: Array Methods — .map(), .filter(), and .reduce(). These build directly on the array shape you learned today, and they are the exact tool React uses to render lists on screen.

💬 Questions? Ask your teacher. Arrays, objects, index vs. key, dot vs. bracket notation — if anything feels unclear, ask me now. Paste your code from the build challenge and I will check it line by line.