Objects in JavaScript

Software developer writing to think clearly about code, systems, and real-world tradeoffs.
When programs start handling real-world data, simple variables are not enough.
You often need to group related information together.
That’s where objects come in.
In JavaScript, objects are used everywhere—from user profiles to configuration settings.
What Objects Are (Simple Idea)
An object stores data as key–value pairs.
Think of it like a dictionary:
| Key | Value |
|---|---|
| name | sudhanshu |
| age | 22 |
| city | Delhi |
In code, this becomes:
const person = {
name: "sudhanshu",
age: 22,
city: "Delhi"
};
Here:
name,age,city→ keys"sudhanshu",22,"Delhi"→ values
Why Objects Are Needed
Imagine storing information about a person.
Without objects:
const name = "sudhanhsu";
const age = 22;
const city = "Delhi";
This quickly becomes messy.
With an object:
const person = {
name: "sudhanshu",
age: 22,
city: "Delhi"
};
Now all related data is grouped together.
Objects help with:
organizing data
representing real-world entities
making code easier to manage
Creating an Object
Objects are created using curly braces {}.
Example:
const person = {
name: "sudhanshu",
age: 22,
city: "Delhi"
};
You can add as many properties as needed.
Another example:
const car = {
brand: "Toyota",
model: "Camry",
year: 2023
};
Accessing Object Properties
There are two ways to access properties.
1. Dot Notation (Most Common)
console.log(person.name);
console.log(person.age);
Output
sudhanshu
22
This is the cleanest and most readable way.
2. Bracket Notation
console.log(person["city"]);
Output
Delhi
Bracket notation is useful when the key is dynamic.
Example:
const key = "name";
console.log(person[key]);
Updating Object Properties
You can change values easily.
person.age = 25;
Now the object becomes:
{
name: "sudhanshu",
age: 25,
city: "Delhi"
}
Adding New Properties
Objects can grow dynamically.
person.country = "India";
Now:
{
name: "sudhanshu",
age: 25,
city: "Delhi",
country: "India"
}
Deleting Properties
You can remove a property using delete.
delete person.city;
Now:
{
name: "sudhanshu",
age: 25,
country: "India"
}
Looping Through Object Keys
To iterate through properties, use for...in.
Example:
const person = {
name: "sudhanshu",
age: 22,
city: "Delhi"
};
for (let key in person) {
console.log(key + ": " + person[key]);
}
Output:
name: sudhanshu
age: 22
city: Delhi
Explanation:
keystores the property nameperson[key]gives the value
Visual Diagram: Object Structure
person
│
├── name → sudhanshu
├── age → 22
└── city → Delhi
Objects behave like labeled containers.
Array vs Object (Important Difference)
Both arrays and objects store data, but they serve different purposes.
| Feature | Array | Object |
|---|---|---|
| Structure | Ordered list | Key-value pairs |
| Access | index | property name |
| Example | [10,20,30] |
{name:"Rahul"} |
Example array:
const numbers = [10, 20, 30];
console.log(numbers[1]);
Example object:
const person = {
name: "Rahul",
age: 22
};
console.log(person.name);
Comparison Diagram
ARRAY
[ "apple", "banana", "mango" ]
0 → apple
1 → banana
2 → mango
OBJECT
{
name: "Rahul",
age: 22,
city: "Delhi"
}
name → Rahul
age → 22
city → Delhi
Assignment
1. Create a Student Object
Create an object with:
name
age
course
Example:
const student = {
name: "Amit",
age: 20,
course: "Computer Science"
};
2. Update One Property
Change the student's age.
Example:
student.age = 21;
3. Print All Keys and Values
Use a loop:
for (let key in student) {
console.log(key + ": " + student[key]);
}
Key Takeaway
Objects are one of the most important structures in JavaScript because they allow programs to represent real-world entities like:
users
products
orders
configurations
Nearly every modern framework built on JavaScript—including React, Node.js, and Vue.js—relies heavily on objects.




