Objects in JavaScript
An object in JavaScript is used to store multiple related values in a single variable. Objects store data in the form of key-value pairs.
For example, a student has related information such as name, age, and course. Instead of creating separate variables, we can store all this information inside one object.
let student = {
name: "John",
age: 20,
course: "JavaScript"
};
console.log(student);
Output:
{
name: "John",
age: 20,
course: "JavaScript"
}
Here, student is an object. name, age, and course are called keys, and "John", 20, and "JavaScript" are called values.
Creating an Object
An object is created using curly braces {}. Each property consists of a key and a value separated by a colon :.
let product = {
name: "Laptop",
price: 50000,
inStock: true
};
In this object:
-
name,price, andinStockare keys. -
"Laptop",50000, andtrueare values.
Each key-value pair is separated by a comma.
Accessing Object Properties
We can access object properties in two ways:
- Dot Notation
- Bracket Notation
1. Dot Notation
Dot notation is the most common way to access an object property.
let product = {
name: "Laptop",
price: 50000
};
console.log(product.name);
console.log(product.price);
Output:
Laptop
50000
Here, product.name accesses the value of the name property.
2. Bracket Notation
Bracket notation uses square brackets [] to access object properties.
let product = {
name: "Laptop",
price: 50000
};
console.log(product["name"]);
console.log(product["price"]);
Output:
Laptop
50000
The property name is written as a String inside the square brackets.
Dot Notation vs Bracket Notation
| Dot Notation | Bracket Notation |
|---|---|
Uses a dot .
|
Uses square brackets []
|
Example: product.name
|
Example: product["name"]
|
| Simple and commonly used | Useful when the property name is dynamic |
| Cannot directly use a variable as a property name | Can use a variable as a property name |
For example, bracket notation is useful when the property name is stored in a variable.
let product = {
name: "Laptop",
price: 50000
};
let key = "name";
console.log(product[key]);
Output:
Laptop
Here, the value of key is "name", so product[key] accesses the name property.
Adding a New Property
We can add a new property to an existing object.
let user = {
name: "John"
};
user.age = 25;
console.log(user);
Output:
{
name: "John",
age: 25
}
Here, the age property is added to the user object.
Updating an Object Property
We can update the value of an existing property by assigning a new value.
let user = {
name: "John",
city: "Chennai"
};
user.city = "Coimbatore";
console.log(user);
Output:
{
name: "John",
city: "Coimbatore"
}
Here, the value of city is changed from "Chennai" to "Coimbatore".
Deleting an Object Property
The delete operator is used to remove a property from an object.
let user = {
name: "John",
age: 25,
city: "Chennai"
};
delete user.age;
console.log(user);
Output:
{
name: "John",
city: "Chennai"
}
Here, the age property is removed from the object.
Object with Different Data Types
An object can store values of different data types.
let employee = {
name: "David",
age: 28,
isWorking: true,
skills: ["HTML", "CSS", "JavaScript"]
};
console.log(employee);
Here, the object contains a String, Number, Boolean, and Array.
Objects Can Contain Functions
An object can also contain a function. A function inside an object is called a method.
let person = {
name: "John",
greet: function() {
console.log("Hello");
}
};
person.greet();
Output:
Hello
Here, greet is a method of the person object.