Understanding Objects in JavaScript

As we start learning JavaScript, we first work with variables and arrays. Arrays are useful when we want to store a list of values in order.
But sometimes data is not just a list — it has different pieces of information related to one thing.
For example, imagine you want to store information about a person.
You might have:
name
age
city
If we try to store this using separate variables, it looks like this:
let name = "Aryan";
let age = 21;
let city = "Nagpur";
This works, but these variables are not grouped together. They represent one person but are stored separately.
This is where objects become useful.
An object lets us store related data together using key-value pairs.
What is an Object?
An object is a collection of key-value pairs.
Example:
let person = {
name: "Aryan",
age: 21,
city: "Nagpur"
};
Here:
name,age, andcityare keys (properties)"Aryan",21,"Nagpur"are values
You can think of an object like a profile card that holds different pieces of information about something.
Creating an Object
Objects are created using curly braces {}.
Example:
let person = {
name: "Aryan",
age: 21,
city: "Nagpur"
};
Each property inside the object follows this format:
key : value
Multiple properties are separated by commas.
Accessing Object Properties
We can access values inside an object in two ways.
1. Dot Notation
This is the most common and simplest way.
console.log(person.name);
console.log(person.age);
Output:
Aryan
21
2. Bracket Notation
Another way is using square brackets.
console.log(person["city"]);
Output:
Nagpur
Bracket notation is useful when the property name is stored inside a variable.
Example:
let key = "name";
console.log(person[key]);
Updating Object Properties
We can change a value by assigning a new one.
Example:
person.age = 22;
console.log(person.age);
Output:
22
Here we simply replaced the old value.
Adding New Properties
Objects are flexible. We can add new properties anytime.
Example:
person.country = "India";
console.log(person);
Now the object becomes:
{
name: "Aryan",
age: 22,
city: "Nagpur",
country: "India"
}
Deleting Properties
If we want to remove a property, we use the delete keyword.
Example:
delete person.city;
console.log(person);
Now the city property is removed.
Looping Through an Object
Sometimes we want to see all properties inside an object.
For that we use the for...in loop.
Example:
let person = {
name: "Aryan",
age: 21,
city: "Nagpur"
};
for (let key in person) {
console.log(key, person[key]);
}
Output:
name Aryan
age 21
city Nagpur
Here:
keyrepresents each property nameperson[key]gives the value
Array vs Object
Beginners often get confused between arrays and objects.
Here is a simple comparison.
| Array | Object |
|---|---|
| Stores ordered values | Stores key-value pairs |
| Access using index | Access using property name |
| Example: list of items | Example: information about one item |
Example array:
let fruits = ["Apple", "Banana", "Mango"];
Example object:
let person = {
name: "Aryan",
age: 21
};
Arrays are useful for lists, while objects are useful for structured information.
Practice Assignment
Try this exercise to understand objects better.
1. Create a Student Object
let student = {
name: "Rahul",
age: 20,
course: "Computer Science"
};
2. Update One Property
student.age = 21;
3. Add a New Property
student.city = "Pune";
4. Print All Keys and Values
for (let key in student) {
console.log(key, student[key]);
}




