Understanding Objects in JavaScript
JavaScript provides several data structures to organize data. One of the most powerful and commonly used structures is the object.
Objects allow us to store related data together in a structured way using key–value pairs.
In this article, we will learn:
What objects are and why they are needed
How to create objects
How to access properties
How to update properties
How to add and delete properties
How to loop through object keys
What Are Objects in JavaScript?
An object is a collection of key-value pairs.
Each value is associated with a key (property name).
Structure
key: value
Example:
let person = {
name: "John",
age: 25,
city: "New York"
};
Here:
| Key | Value |
|---|---|
| name | "John" |
| age | 25 |
| city | "New York" |
So the object person stores information about a person.
Why Are Objects Needed?
Objects help us group related information together.
Imagine storing information about a person without objects:
let name = "John";
let age = 25;
let city = "New York";
These variables are not connected.
Using an object makes the relationship clear:
let person = {
name: "John",
age: 25,
city: "New York"
};
Now all information belongs to one entity: the person.
Objects vs Arrays
Both objects and arrays store multiple values, but they work differently.
| Feature | Array | Object |
|---|---|---|
| Data structure | Ordered list | Key-value pairs |
| Access method | Index | Property name |
| Example | arr[0] |
obj.name |
Array Example
let colors = ["red", "blue", "green"];
console.log(colors[0]);
Output
red
Object Example
let person = {
name: "John",
age: 25
};
console.log(person.name);
Output
John
Creating Objects
Objects are created using curly braces {}.
Example
let person = {
name: "Alice",
age: 22,
city: "London"
};
console.log(person);
Output
{ name: 'Alice', age: 22, city: 'London' }
Each property is written as:
key: value
Accessing Object Properties
There are two ways to access object properties.
1. Dot Notation
This is the most common method.
let person = {
name: "Alice",
age: 22
};
console.log(person.name);
Output
Alice
2. Bracket Notation
Bracket notation is useful when the key is stored in a variable.
let person = {
name: "Alice",
age: 22
};
console.log(person["age"]);
Output
22
Using Variables
let key = "name";
console.log(person[key]);
Output
Alice
Updating Object Properties
You can change a property value using assignment.
let person = {
name: "Alice",
age: 22
};
person.age = 23;
console.log(person);
Output
{ name: 'Alice', age: 23 }
Adding New Properties
New properties can be added simply by assigning a value.
let person = {
name: "Alice",
age: 22
};
person.city = "London";
console.log(person);
Output
{ name: 'Alice', age: 22, city: 'London' }
Deleting Properties
The delete keyword removes a property from an object.
let person = {
name: "Alice",
age: 22,
city: "London"
};
delete person.city;
console.log(person);
Output
{ name: 'Alice', age: 22 }
Looping Through Object Keys
To iterate through object properties, we use the for...in loop.
Example
let person = {
name: "Alice",
age: 22,
city: "London"
};
for (let key in person) {
console.log(key, person[key]);
}
Output
name Alice
age 22
city London
Explanation:
keyrepresents the property nameperson[key]gives the value
Mind Map -
Assignment: Student Object
Let's create an object representing a student.
Step 1: Create the Object
let student = {
name: "Rahul",
age: 20,
course: "Computer Science"
};
Step 2: Update a Property
student.age = 21;
Step 3: Print All Keys and Values
for (let key in student) {
console.log(key + ":", student[key]);
}
Output
name: Rahul
age: 21
course: Computer Science