Understanding Objects in JavaScript

When programs start dealing with real data, simple variables are not enough.
Imagine you want to store information about a person:
name
age
city
You could store them in separate variables:
let name = "Akhil"
let age = 22
let city = "Bengaluru"
This works, but it becomes messy when the data grows.
This is where objects help.
An object allows us to group related data together in a single structure.
What Are Objects in JavaScript?
An object is a collection of key–value pairs.
Each piece of data contains:
a key -the name of the property
a value -the actual data
Example:
const person = {
name: "Akhil",
age: 22,
city: "bengaluru"
}
Here:
name,age, andcityare keys"Akhil",22,"Delhi"are values
This makes it easier to represent real-world data.
Creating Object
Objects are created using curly braces {}.
Example:
const person = {
name: "Akhil",
age: 22,
city: "Delhi"
}
Each property inside the object is separated by a comma.
Objects can store different types of values:
const user = {
username: "akhil",
age: 22,
isStudent: true
}
Accessing Object Properties
There are two ways to access values from an object.
1. Dot Notation
This is the most common method.
console.log(person.name)
console.log(person.age)
Output:
Akhil
22
2. Bracket Notation
We can also access properties using square brackets.
console.log(person["city"])
Output:
Bengaluru
Bracket notation is useful when the property name is dynamic.
Example:
const key = "name"
console.log(person[key])
Updating Object property
Updating a property is very simple.
Just assign a new value.
person.age = 23
console.log(person.age)
Output:
23
JavaScript objects are mutable, it means their values can be changed.
Adding new property
We can also add new properties to an object.
Example:
person.country = "India"
console.log(person)
Now the object becomes:
{
name: "Akhil",
age: 23,
city: "Bengaluru",
country: "India"
}
Deleting Object properties
If we want to remove a property, we use the delete keyword.
Example:
delete person.city
Now the city property is removed from the object.
Looping in objects
Sometimes we want to print all properties inside an object.
We can do this using a for...in loop.
Example:
const person = {
name: "Akhil",
age: 22,
city: "bengaluru"
}
for (let key in person) {
console.log(key, person[key])
}
Output:
name Akhil
age 22
city bengaluru
The loop gives us each key, and we can use that key to access its value.
Array v/s Objects
Array:
const fruits = ["apple", "banana", "mango"]
Access using index:
fruits[0]
fruits[1]
Object:
const person = {
name: "Akhil",
age: 22
}
Access using keys:
person.name
person.age
Arrays use indexes, objects use keys.
Assignment
Try these exercises to practice.
1. Create a Student Object
Create an object representing a student.
2. Update a Property
Update the student's age.
3. Add a New Property
Add a property called city.
4. Print All Keys and Values
Use a loop to print all properties.
Conclusion
Objects are one of the most important concepts in JavaScript.They help us represent real-world data in a structured way. Once you get comfortable with objects, many important topics like:
APIs
JSON
databases
frameworks
become much easier to understand. I hope this blog provide some value in your knowledge see you with another topic till then keep learning


