Understanding Objects in JavaScript

“If variables are containers, objects are structured containers”
So far, we’ve seen how JavaScript stores simple values.
Javascript variables and data types
But real applications don’t deal with isolated values — they deal with related data grouped together.
User ho, product ho, order ho — sab multiple values ka combination hote hain.
That’s where objects come in.
What is an Object in JavaScript?
An Object is a collection of key-value pairs.
Instead of storing everything separately:
let name = "Mohit";
let age = 24;
let city = "Delhi";
We group them into one structure:
const user = {
name: "Mohit",
age: 24,
city: "Delhi"
};
This is cleaner, structured, and scalable.
Why Do We Need Objects?
Objects help you:
Group related data
Represent real-world entities
Manage structured information easily
Think of it like:
A user, product, or order is not one value — it’s a collection of properties.
Object Structure (Key–Value Model)
An object is made of:
Key (property name)
Value (data)
const user = {
name: "Mohit", // key: name → value: "Mohit"
age: 24, // key: age → value: 24
isStudent: true
};
Keys are usually strings
Values can be any data type (string, number, array, object, function)
Creating Objects
1. Object Literal (most common)
const user = {
name: "Mohit",
age: 24
};
2. Using new Object()
const user = new Object();
user.name = "Mohit";
user.age = 24;
3. Empty Object + Add Later
const user = {};
user.name = "Mohit";
user.age = 24;
All three create objects — literal syntax is preferred.
Accessing Object Properties
1. Dot Notation (most common)
console.log(user.name); // Mohit
2. Bracket Notation
console.log(user["age"]); // 24
When to use bracket notation?
When key is dynamic
When key has spaces
const obj = {
"full name": "Mohit Kumar"
};
console.log(obj["full name"]);
Updating Object Properties
user.age = 25;
If property exists → it gets updated
If not → it gets created
Adding New Properties
user.city = "Delhi";
Now:
{
name: "Mohit",
age: 25,
city: "Delhi"
}
Deleting Properties
delete user.city;
Removes the property completely
Looping Through Object Keys
Objects are not directly iterable like arrays, so we use loops.
Using for...in
for (let key in user) {
console.log(key, user[key]);
}
Output:
name Mohit
age 25
Using Object.keys()
const keys = Object.keys(user);
keys.forEach((key) => {
console.log(key, user[key]);
});
Using Object.values()
const values = Object.values(user);
console.log(values);
Using Object.entries()
Object.entries(user).forEach(([key, value]) => {
console.log(key, value);
});
Object vs Array (Important Difference)
Array
const arr = ["Mohit", 24, "Delhi"];
Ordered
Access using index
Best for list-like data
Object
const user = {
name: "Mohit",
age: 24,
city: "Delhi"
};
Key-based access
No strict order relevance
Best for structured data
Real-World Example
const student = {
name: "Rahul",
age: 20,
course: "BCA"
};
student.age = 21;
for (let key in student) {
console.log(key, student[key]);
}
Final Thought
Objects aren’t just a data type — they’re how I think about storing real-world structured data in JavaScript, as key-value pairs grouped in one place.




