You already know how to store a single value in a variable — const name = "Alice". But what if you need to store everything about Alice in one place — her name, age, and city? That's exactly what objects are for.
An object is a collection of related data, grouped together under one name using key-value pairs.
What Is an Object?
Think of an object like a contact card in your phone. One card holds multiple pieces of information about one person:
┌─────────────────────────────┐
│ name → "Alice" │
│ age → 25 │
│ city → "Mumbai" │
└─────────────────────────────┘
Each piece of information has a key (like name) and a value (like "Alice"). Together, they form a key-value pair.
Creating an Object
Use curly braces {} with key: value pairs separated by commas:
const person = {
name: "Alice",
age: 25,
city: "Mumbai"
};
-
name,age,cityare the keys (also called properties) -
"Alice",25,"Mumbai"are the values - Values can be any data type — string, number, boolean, array, even another object
Accessing Properties
Dot notation — the most common way
console.log(person.name); // "Alice"
console.log(person.age); // 25
console.log(person.city); // "Mumbai"
Bracket notation — useful when the key is dynamic
console.log(person["name"]); // "Alice"
const key = "age";
console.log(person[key]); // 25 — key stored in a variable
Use dot notation by default. Switch to bracket notation when the key comes from a variable or contains special characters.
Updating Object Properties
You can change the value of any property directly:
const person = {
name: "Alice",
age: 25,
city: "Mumbai"
};
person.age = 26;
person.city = "Pune";
console.log(person.age); // 26
console.log(person.city); // "Pune"
Even though person is declared with const, you can still modify its properties. const only prevents you from replacing the entire object — not from changing what's inside it.
person = { name: "Bob" }; // Error — cannot reassign a const
person.name = "Bob"; // Fine — updating a property
Adding and Deleting Properties
Add a new property
Just assign to a key that doesn't exist yet:
person.email = "alice@example.com";
console.log(person.email); // "alice@example.com"
Delete a property
delete person.city;
console.log(person.city); // undefined — property is gone
Objects Can Have Methods
A method is a function stored as a property of an object:
const person = {
name: "Alice",
age: 25,
greet() {
return "Hi, I'm " + this.name + "!";
}
};
console.log(person.greet()); // "Hi, I'm Alice!"
this inside a method refers to the object itself — so this.name gives you "Alice".
Looping Through Object Keys
Use for...in to iterate over every key in an object:
const person = {
name: "Alice",
age: 25,
city: "Mumbai"
};
for (const key in person) {
console.log(key + ": " + person[key]);
}
// name: Alice
// age: 25
// city: Mumbai
Useful object utility methods
console.log(Object.keys(person));
// ["name", "age", "city"]
console.log(Object.values(person));
// ["Alice", 25, "Mumbai"]
console.log(Object.entries(person));
// [["name", "Alice"], ["age", 25], ["city", "Mumbai"]]
Nested Objects
Objects can contain other objects:
const student = {
name: "Rahul",
age: 20,
address: {
city: "Delhi",
pincode: "110001"
}
};
console.log(student.address.city); // "Delhi"
console.log(student.address.pincode); // "110001"
Destructuring — Extract Values Cleanly
Instead of writing person.name, person.age separately, pull them out in one line:
const { name, age, city } = person;
console.log(name); // "Alice"
console.log(age); // 25
console.log(city); // "Mumbai"
Array vs Object — Which to Use?
Array []
|
Object {}
|
|
|---|---|---|
| Structure | Ordered list | Named key-value pairs |
| Access | By index: arr[0]
|
By key: obj.name
|
| Best for | List of similar items | One thing with multiple properties |
| Example | List of 5 scores | One student's full profile |
// Array — a list of students
const students = ["Alice", "Bob", "Charlie"];
// Object — one student's details
const student = {
name: "Alice",
age: 20,
grade: "A"
};
// Real-world combo — array of objects (most common pattern!)
const allStudents = [
{ name: "Alice", grade: "A" },
{ name: "Bob", grade: "B" },
];
Top comments (0)