Understanding Objects in JavaScript
When working with JavaScript, we often need to store multiple related pieces of information together. For example, details about a person such as their name, age, and city.
Instead of storing these values separately, JavaScript provides objects to group related data together.
What Are Objects and Why Are They Needed?
An object is a collection of key-value pairs used to store related data.
Think of an object like a profile card.
Example: A person profile
Name → Ram
Age → 22
City → Pune
In JavaScript, this can be stored as an object.
let person = {
name: "Ram",
age: 22,
city: "Pune"
};
console.log(person);
Here:
name,age, andcityare keys"Ram",22,"Pune"are values
Creating Objects
Objects are created using curly braces {}.
Example:
let student = {
name: "Suraj",
age: 21,
course: "Computer Science"
};
console.log(student);
This object stores information about a student.
Accessing Object Properties
There are two ways to access object properties.
1. Dot Notation
The most common method.
console.log(student.name);
console.log(student.age);
Output
Suraj
21
2. Bracket Notation
Bracket notation is useful when the key is stored in a variable.
console.log(student["course"]);
Output
Computer Science
Both methods access the same data.
Updating Object Properties
Object values can be updated easily.
Example:
student.age = 22;
console.log(student.age);
Output
22
Here we changed the age from 21 to 22.
Adding New Properties
We can also add new properties to an object.
Example:
student.city = "Pune";
console.log(student);
Now the object contains:
name
age
course
city
Deleting Properties
To remove a property, use the delete keyword.
Example:
delete student.city;
console.log(student);
Now the city property is removed.
Looping Through Object Keys
Sometimes we want to print all keys and values of an object.
We can use a for...in loop.
Example:
let student = {
name: "Suraj",
age: 21,
course: "Computer Science"
};
for (let key in student) {
console.log(key + ": " + student[key]);
}
Output
name: Suraj
age: 21
course: Computer Science
This loop goes through every property in the object.
Difference Between Array and Object
| Array | Object |
|---|---|
| Stores ordered data | Stores key-value pairs |
| Accessed by index | Accessed by key |
Example: [10, 20, 30] |
Example: {name:"Ram"} |
Example array:
let numbers = [10, 20, 30];
console.log(numbers[0]);
Example object:
let person = {
name: "Ram",
age: 22
};
console.log(person.name);
Arrays are good for lists, while objects are good for structured data.
Example
Create an object representing a student.
let student = {
name: "Suraj",
age: 21,
course: "Web Development"
};
Update one property:
student.age = 22;
Print all keys and values:
for (let key in student) {
console.log(key + ": " + student[key]);
}
Conclusion
Objects are one of the most important features in JavaScript. They allow developers to store related information in a structured way using key-value pairs.
Key things to remember:
Objects store data as key-value pairs
Use dot notation or bracket notation to access properties
Properties can be updated, added, or deleted
for...inloops help iterate through object keys
Understanding objects will help you build more organized and scalable JavaScript applications.