Understanding Objects in JavaScript
What Are Objects?
Objects store related data using:
key → value
pairs.
They help organize real-world entities.
Real-World Example
A person has:
Name
Age
City
Instead of separate variables:
const name = "Ali"; const age = 22; const city = "Lucknow";
we group them into one object.
Creating an Object
const person = {
name: "Ali",
age: 22,
city: "Lucknow"
};
Object Structure Visualization
Objects Are Key-Value Structures
Accessing Properties
1. Dot Notation
console.log(person.name); console.log(person.age);
Output
Ali 22
2. Bracket Notation
console.log(person["city"]);
Output
Lucknow
Dot vs Bracket Notation
Updating Object Properties
person.age = 23; console.log(person.age);
Output
23
Adding New Properties
person.country = "India"; console.log(person);
Output
{
name: "Ali",
age: 23,
city: "Lucknow",
country: "India"
}
Deleting Properties
delete person.city; console.log(person);
Output
{
name: "Ali",
age: 23,
country: "India"
}
Looping Through Object Keys
Use:
for...in
Example
const person = {
name: "Ali",
age: 22,
city: "Lucknow"
};
for (let key in person) {
console.log(key, person[key]);
}
Output
name Ali age 22 city Lucknow
How Looping Works
Array vs Object
Array
Stores ordered values.
const fruits = ["apple", "banana"];
Accessed using indexes.
Object
Stores named values.
const person = {
name: "Ali"
};
Accessed using keys.
Comparison Diagram
Array vs Object Table
Practical Example
const car = {
brand: "Toyota",
model: "Camry",
year: 2024
};
console.log(car.brand);
car.year = 2025;
console.log(car);
Assignment
Step 1: Create Student Object
const student = {
name: "Sara",
age: 20,
course: "Computer Science"
};
Step 2: Update Property
student.age = 21;
Step 3: Loop Through Object
for (let key in student) {
console.log(key, student[key]);
}
Common Beginner Mistakes
Forgetting Quotes in Bracket Notation
Incorrect:
person[name]
Correct:
person["name"]
Using Array Syntax for Objects
Incorrect:
person[0]
Objects use keys, not indexes.
Key Takeaways
Final Notes
Objects are one of the most important concepts in JavaScript because almost everything in JavaScript is built around objects.
Used heavily in:
APIs
React
Databases
Backend development
Frontend applications
No attachments.
0 Comments
Sign in to join the conversation
No comments yet. Be the first to comment!