Objects
Group related data using key-value pairs.
Objects are one of the most important concepts in JavaScript. They allow you to store related information together in a structured way. While arrays are used to store multiple values in a list, objects are used to store data as key-value pairs.
Think of an object as a digital profile card. A person may have a name, age, email, and phone number. Instead of storing each piece of information in separate variables, an object allows you to group everything together.
Objects are widely used in web development because they help organize data and represent real-world entities such as users, products, orders, and settings.
What Is an Object?
An object is a collection of related data stored as properties.
Each property consists of:
- A key, also called the property name
- A value, also called the property value
let user = {
name: "John",
age: 25,
city: "Delhi"
};In this object:
- name is a key and John is its value.
- age is a key and 25 is its value.
- city is a key and Delhi is its value.
Creating an Object
Objects are created using curly braces.
let product = {
name: "Laptop",
price: 50000,
inStock: true
};This object contains information about a product.
Accessing Object Properties
There are two common ways to access object properties.
Dot Notation
console.log(product.name);Output:
LaptopBracket Notation
console.log(product["price"]);Output:
50000Both methods are commonly used in JavaScript.
Updating Object Properties
You can change the value of an existing property.
let user = {
name: "John",
age: 25
};
user.age = 26;
console.log(user.age);Output:
26JavaScript allows objects to be modified easily.
Adding New Properties
New properties can be added at any time.
let user = {
name: "John"
};
user.email = "john@example.com";
console.log(user);Now the object contains an additional property called email.
Object Methods
Objects can also contain functions. When a function is stored inside an object, it is called a method.
let user = {
name: "John",
greet: function() {
console.log("Hello!");
}
};
user.greet();Output:
Hello!Methods allow objects to perform actions.
Looping Through Object Properties
The for...in loop can be used to access object properties.
let user = {
name: "John",
age: 25
};
for (let key in user) {
console.log(key, user[key]);
}Output:
name John
age 25This is useful when you need to work with all properties of an object.
Real-World Example
Imagine an online shopping website.
let product = {
name: "Smartphone",
price: 25000,
brand: "Samsung",
inStock: true
};
console.log(product.name);Output:
SmartphoneInstead of creating separate variables for each detail, the object keeps all product information together in one place.
Summary
Objects are used to store related data as key-value pairs. They help developers organize information in a structured and meaningful way. JavaScript objects can store properties, methods, and even other objects, making them extremely powerful and flexible. Since most real-world applications rely heavily on objects, learning how to create, access, update, and manage them is an essential JavaScript skill.