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
| Term | Meaning |
|---|---|
| Array | An ordered list of values, written with [ ] |
| Element / Item | One value stored inside an array |
| Index | The position of an item — starts at 0, not 1 |
| .length | A 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
| Term | Meaning |
|---|---|
| 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 — use when the key name is fixed text |
| Bracket notation | object["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
| Mistake | Fix |
|---|---|
Reading fruits[fruits.length] | The last valid index is fruits.length - 1 |
| Confusing key with value | The 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 |