Reference Document

Arrays & Objects

Quick reference for Lesson 12. Print this and pin it next to your monitor.

Array — Ordered List

Create, read, count, add, remove

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

fruits[0];          // "apple"  — first item, index 0
fruits[2];          // "mango"  — last item, index length - 1
fruits.length;       // 3        — item count

fruits.push("kiwi"); // add to the end
fruits.pop();         // remove from the end
TermMeaning
ArrayAn ordered list of values, written with [ ]
Element / ItemOne value stored inside an array
IndexThe position of an item — starts at 0, not 1
.lengthA count of items in the array

Object — Named Values

Create, read (two ways), update, add

const user = { name: "Sara", age: 30 };

user.name;        // "Sara"  — dot notation
user["name"];     // "Sara"  — bracket notation

user.age = 31;    // update an existing property
user.city = "Pune"; // add a new property

Nested objects

const user = {
  name: "Sara",
  address: { city: "Pune", zip: "411001" }
};

user.address.city; // "Pune" — chain the dots
TermMeaning
ObjectA set of named values, written with { }
KeyThe name given to a stored value
PropertyOne key and its value, together
Dot notationobject.key — use when the key name is fixed text
Bracket notationobject["key"] or object[variable] — use when the key name comes from a variable, or has spaces

Array of Objects — the Real-World Shape

Most API responses and database records look like this

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

contacts[0];        // { name: "Sara", phone: "555-0101" }
contacts[0].name;   // "Sara"
contacts[1].phone;  // "555-0192"

Looping — for...of

Visit every item in an array

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

Array methods like .map(), .filter(), and .reduce() replace most loops in real codebases. They are covered in the next lesson.

Common Mistakes

MistakeFix
Reading fruits[fruits.length]The last valid index is fruits.length - 1
Confusing key with valueThe key is the name; the value is what it stores
Using [ ] when you mean { }Square brackets are for lists; curly braces are for named values