Understanding Objects in JavaScript
JavaScript objects are one of the most important concepts in programming. Almost everything in JavaScript revolves around objects. If you understand objects well, writing real-world applications becomes much easier.
In this blog, we will learn what objects are, why they are needed, how to create them, access their properties, update them, and loop through them using simple examples.
1. What Are Objects in JavaScript?
An object is a data structure that stores information in key–value pairs.
Think of an object like a real-life person profile.
Example:
Name → Kanishka
Age → 21
City → Lucknow
Here:
Name, Age, City are the keys (properties)
Kanishka, 21, Lucknow are the values
In JavaScript, this looks like:
let person = {
name: "Kanishka",
age: 21,
city: "Lucknow"
};
So the structure becomes:
key → value
name → "Kanishka"
age → 21
city → "Lucknow"
This makes objects perfect for storing structured data about a single entity.
2. Why Do We Need Objects?
Imagine storing person data using variables:
let name = "Kanishka";
let age = 21;
let city = "Lucknow";
This works for small data, but what if you have 100 students?
Managing separate variables becomes messy.
Objects solve this by grouping related data together.
Example:
let student = {
name: "Kanishka",
age: 21,
course: "B.Tech"
};
Now everything related to the student is inside one structure.
3. Creating Objects
There are multiple ways to create objects, but the simplest way is using curly braces {}.
Example:
let person = {
name: "Rahul",
age: 25,
city: "Delhi"
};
Here:
personis the object namename,age,cityare propertiesValues are assigned using
:.
4. Accessing Object Properties
We can access object properties in two ways.
1. Dot Notation
This is the most common and readable way.
console.log(person.name);
console.log(person.age);
Output:
Rahul
25
Structure:
objectName.property
Example:
person.city
2. Bracket Notation
Another way is using square brackets.
console.log(person["name"]);
console.log(person["city"]);
This is useful when the property name is stored in a variable.
Example:
let key = "age";
console.log(person[key]);
5. Updating Object Properties
You can easily change a property value.
Example:
person.age = 26;
Now the object becomes:
{
name: "Rahul",
age: 26,
city: "Delhi"
}
6. Adding New Properties
Objects are flexible. You can add new properties anytime.
Example:
person.country = "India";
Now the object becomes:
{
name: "Rahul",
age: 26,
city: "Delhi",
country: "India"
}
7. Deleting Properties
You can also remove properties using the delete keyword.
Example:
delete person.city;
Now the object becomes:
{
name: "Rahul",
age: 26,
country: "India"
}
8. Looping Through Object Keys
To access all properties in an object, we use a for...in loop.
Example:
let person = {
name: "Rahul",
age: 25,
city: "Delhi"
};
for (let key in person) {
console.log(key + " : " + person[key]);
}
Output:
name : Rahul
age : 25
city : Delhi
This loop goes through every key in the object.
9. Object vs Array (Important Difference)
Many beginners confuse arrays and objects.
| Feature | Array | Object |
|---|---|---|
| Data Structure | Ordered list | Key-value pairs |
| Access Method | Index number | Property name |
| Example | ["Apple", "Banana"] |
{name: "Rahul"} |
Example of Array:
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[0]);
Example of Object:
let person = {
name: "Rahul",
age: 25
};
console.log(person.name);
10. Visual Representation of Object
person
│
├── name → "Rahul"
├── age → 25
└── city → "Delhi"
You can imagine an object like a dictionary or data card storing related information.
Assignment (Practice Task)
Create an object representing a student.
Step 1: Create the object
let student = {
name: "Aman",
age: 20,
course: "B.Tech"
};
Step 2: Update one property
student.age = 21;
Step 3: Add a new property
student.city = "Noida";
Step 4: Print all keys and values
for (let key in student) {
console.log(key + " : " + student[key]);
}
Final Thoughts
Objects are one of the most powerful features of JavaScript. They allow developers to organize related data in a clean and structured way.
Key takeaways:
Objects store data as key-value pairs
Use dot notation or bracket notation to access properties
Properties can be added, updated, or deleted
Use for...in loop to iterate through object keys
Once you understand objects well, you will be able to work with APIs, JSON data, and real-world applications much more easily.


