In many programs, we need to represent real-world entities such as a person, a student, a product, or a car. These entities often have multiple related properties.
JavaScript provides objects to organize this type of data in a clear and structured way.
What Are Objects and Why Do We Need Them?
An object is a data structure that stores information as key-value pairs.
Each piece of data has:
- a key (also called a property name)
- a value (the data stored in that property)
Example:
let person = {
name: "Alice",
age: 25,
city: "London"
};
Here:
| Key | Value |
|---|---|
| name | "Alice" |
| age | 25 |
| city | "London" |
Objects help us:
- Represent real-world data
- Organize related information together
- Access and update data easily
Array vs Object
It is helpful to understand the difference between arrays and objects.
Array
Arrays store ordered values and are accessed using indexes.
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[0]); // Apple
Object
Objects store named values and are accessed using keys.
let person = {
name: "Alice",
age: 25
};
console.log(person.name); // Alice
Summary:
| Feature | Array | Object |
|---|---|---|
| Structure | Ordered list | Key-value pairs |
| Access | Index (0,1,2) | Key name |
| Use case | Lists of values | Structured data |
Creating Objects
Objects are created using curly braces {}.
Example:
let person = {
name: "John",
age: 30,
city: "New York"
};
Each property is written as:
key: value
Multiple properties are separated by commas.
Accessing Properties
There are two ways to access object properties:
- Dot notation
- Bracket notation
Dot Notation
This is the most common way.
let person = {
name: "John",
age: 30
};
console.log(person.name);
console.log(person.age);
Output:
John
30
Bracket Notation
Properties can also be accessed using brackets.
let person = {
name: "John",
age: 30
};
console.log(person["name"]);
console.log(person["age"]);
Output:
John
30
Bracket notation is useful when the property name is stored in a variable.
Example:
let key = "name";
console.log(person[key]);
Updating Object Properties
You can update the value of an existing property.
Example:
let person = {
name: "John",
age: 30
};
person.age = 31;
console.log(person);
Output:
{ name: "John", age: 31 }
Adding New Properties
New properties can be added easily.
Example:
let person = {
name: "John",
age: 30
};
person.city = "Paris";
console.log(person);
Output:
{ name: "John", age: 30, city: "Paris" }
Deleting Properties
Properties can be removed using the delete keyword.
Example:
let person = {
name: "John",
age: 30,
city: "Paris"
};
delete person.city;
console.log(person);
Output:
{ name: "John", age: 30 }
Looping Through Object Keys
To go through all properties of an object, we can use a for...in loop.
Example:
let person = {
name: "Alice",
age: 25,
city: "London"
};
for (let key in person) {
console.log(key, person[key]);
}
Output:
name Alice
age 25
city London
Explanation:
-
keyrepresents each property name -
person[key]retrieves its value
Assignment
Questions
Create an object representing a student.
Add properties such as:
- name
- age
- course
Update one property.
Print all keys and values using a loop.
Answers
1. Create a Student Object
let student = {
name: "Rahul",
age: 20,
course: "Computer Science"
};
2. Update One Property
student.age = 21;
console.log(student);
Output:
{ name: "Rahul", age: 21, course: "Computer Science" }
3. Print All Keys and Values
for (let key in student) {
console.log(key, student[key]);
}
Output:
name Rahul
age 21
course Computer Science
Conclusion
Objects are an essential part of JavaScript because they allow us to represent structured data using key-value pairs. They are commonly used to represent real-world entities such as users, products, or students.
By learning how to create objects, access properties, update values, and loop through keys, you build a strong foundation for working with more complex data in JavaScript applications.
Top comments (0)