Understanding Objects in JavaScript
Objects are one of the most important concepts in JavaScript. Almost everything in JavaScript revolves around objects in some way.
Whether you are building websites, APIs, games, or applications, objects help organize and manage data efficiently.
In this beginner-friendly guide, you’ll learn:
What objects are
Why objects are needed
How to create objects
Accessing and updating properties
Adding and deleting properties
Looping through object keys
Difference between arrays and objects
By the end, you’ll be comfortable working with JavaScript objects confidently.
What Are Objects in JavaScript?
An object is a collection of related data stored as key-value pairs.
Think of an object like a real-world person profile.
A person has:
Name
Age
City
These pieces of information belong together.
In JavaScript, we can store them inside an object.
Real-World Example of an Object
const person = {
name: "Hitesh",
age: 22,
city: "Delhi"
};
Here:
| Key | Value |
|---|---|
name |
"Hitesh" |
age |
22 |
city |
"Delhi" |
The object groups related information together.
Visual Representation of Object Structure
person
│
├── name → "Hitesh"
├── age → 22
└── city → "Delhi"
Why Are Objects Needed?
Without objects, storing related data becomes messy.
Example without object:
const name = "Hitesh";
const age = 22;
const city = "Delhi";
This works for small programs, but becomes difficult to manage in larger applications.
Objects help by:
Organizing related data
Improving readability
Making code easier to maintain
Representing real-world entities
Creating Objects in JavaScript
Objects are created using curly braces {}.
Basic Syntax
const objectName = {
key: value
};
Example
const car = {
brand: "Toyota",
model: "Camry",
year: 2024
};
Understanding Key-Value Pairs
Objects store data in pairs:
key : value
Example:
name: "Hitesh"
name→ key"Hitesh"→ value
Accessing Object Properties
There are two main ways to access object values:
Dot notation
Bracket notation
1. Dot Notation
Syntax
objectName.property
Example
const person = {
name: "Hitesh",
age: 22
};
console.log(person.name);
Output:
Hitesh
2. Bracket Notation
Syntax
objectName["property"]
Example
console.log(person["age"]);
Output:
22
Difference Between Dot and Bracket Notation
| Dot Notation | Bracket Notation |
|---|---|
| Easy to read | More flexible |
| Uses direct property name | Uses string |
| Commonly used | Useful for dynamic keys |
Example of Dynamic Property Access
const key = "city";
const person = {
city: "Delhi"
};
console.log(person[key]);
Output:
Delhi
Updating Object Properties
You can change existing values easily.
Example
const person = {
name: "Hitesh",
age: 22
};
person.age = 23;
console.log(person);
Output:
{ name: 'Hitesh', age: 23 }
Adding New Properties
Objects are dynamic, meaning new properties can be added anytime.
Example
const person = {
name: "Hitesh"
};
person.city = "Delhi";
console.log(person);
Output:
{ name: 'Hitesh', city: 'Delhi' }
Deleting Properties
You can remove properties using the delete keyword.
Example
const person = {
name: "Hitesh",
age: 22
};
delete person.age;
console.log(person);
Output:
{ name: 'Hitesh' }
Looping Through Object Keys
Objects often contain many properties.
JavaScript provides loops to access all keys and values.
Using for...in Loop
const person = {
name: "Hitesh",
age: 22,
city: "Delhi"
};
for (let key in person) {
console.log(key, person[key]);
}
Output:
name Hitesh
age 22
city Delhi
Understanding the Loop
The loop:
Picks one key at a time
Accesses its value
Prints both
This is very useful when working with dynamic data.
Array vs Object
Beginners often confuse arrays and objects.
Both store data, but they work differently.
Array Example
const fruits = ["apple", "banana", "mango"];
Arrays store ordered data.
Access happens using indexes:
console.log(fruits[0]);
Output:
apple
Object Example
const person = {
name: "Hitesh",
city: "Delhi"
};
Objects store named properties.
Access happens using keys:
console.log(person.name);
Output:
Hitesh
Comparison Diagram: Array vs Object
ARRAY
------
[ "apple", "banana", "mango" ]
0 1 2
OBJECT
-------
{
name: "Hitesh",
city: "Delhi"
}
When to Use Arrays vs Objects
Use Arrays When:
Order matters
You store lists
Data is similar
Example:
["apple", "banana", "mango"]
Use Objects When:
Data has labels
You represent entities
Properties have meaning
Example:
{
name: "Hitesh",
age: 22
}
Common Beginner Mistakes
1. Forgetting Quotes in Bracket Notation
Wrong:
person[name]
Correct:
person["name"]
2. Using Dot Notation with Spaces
Wrong:
person.first name
Correct:
person["first name"]
Assignment Practice
Try this yourself.
Task
Create an object representing a student.
Requirements:
Add properties:
name
age
course
Update one property
Print all keys and values using a loop
Example Solution
const student = {
name: "Rahul",
age: 20,
course: "Computer Science"
};
student.age = 21;
for (let key in student) {
console.log(key, student[key]);
}
Real-World Use of Objects
Objects are everywhere in JavaScript applications.
They are used in:
User profiles
API responses
Database records
Shopping carts
Authentication systems
Popular companies like Amazon and Meta heavily rely on object-based data structures in their applications.
Final Thoughts
Objects are one of the foundations of JavaScript.
You learned how to:
✅ Create objects ✅ Access properties ✅ Update values ✅ Add and delete properties ✅ Loop through object keys ✅ Understand arrays vs objects
Mastering objects is essential before moving to advanced JavaScript topics.
The more you practice with real-world examples, the more natural objects will become.
