Objects — describing one real thing
1 min read
Some data belongs together
An array is great for a list of similar things. But what about one thing that has many details — like a single user with a name, age and email? For that we use an object.
let user = {
name: "Aisha",
age: 25,
isPro: true
};
Instead of position numbers, each value here has a key — a label.
How do I read a value?
Use a dot and the key:
console.log(user.name); // Aisha
console.log(user.age); // 25
How do I change or add a detail?
user.age = 26; // change an existing one
user.city = "Delhi"; // add a brand new one
Why are objects so important?
Because real data is almost always objects. Every product, post, message and profile on this very website is an object — keys and values. Arrays and objects together can describe almost any real information:
let users = [
{ name: "Aisha", age: 25 },
{ name: "Rahul", age: 30 }
];
console.log(users[1].name); // Rahul
A list (array) of details (objects) — that is exactly how real apps store their data.
Array = a numbered list. Object = a labelled description of one thing. Mix the two and you can model almost anything.