What is an Object?
- An Object is a non-primitive data type used to store data in key-value pairs.
- It is a dynamic data structure, meaning properties can be added, updated, or removed at runtime.
- Objects are mutable, which means their contents can be changed after creation.
Why do we use Objects?
- Objects are used to store related data and behavior in a single variable. Instead of creating multiple separate variables, we can group them together using an object.
Without Object:
// Without Object
let name="Raksha";
let age=21;
let location="Chennai";
console.log(name);
console.log(age);
console.log(location);
Output:
Raksha
21
Chennai
With Object:
// With Objects
let student = {
name: "Raksha",
age: 22,
city: "Chennai"
};
console.log(student);
Output:
{ name: 'Raksha', age: 22, city: 'Chennai' }
Advantages of objects:
- Used to organize the related data in a single variable.
- Easy to access and update data.
- Can store different types of value.
- Can store functions.
Creating Objects in JavaScript:
Example:
let student = {
name: "Raksha",
age: 22,
city: "Chennai"
};
console.log(student);
Output:
{ name: 'Raksha', age: 22, city: 'Chennai' }
Accessing Objects in JavaScript:
Once an object is created, we can access its properties and methods using two main ways:
1. Dot notation (.)
2. Bracket notation ([])
1. Dot Notation:
We use the dot (.) followed by the property name.
syntax:
object.property
Example:
// Dot Notation
let home={
NoofMembers:4,
type:"Independent House",
noofrooms:4,
location:"Thiruvarur"
}
console.log(home.NoofMembers);
console.log(home.location);
2. Bracket Notation:
We use square brackets [] and specify the property name as a string or symbol.
Syntax:
object["property"]
Example:
// Bracket Notation
let house={
NoofMembers:4,
type:"Independent House",
noofrooms:4,
location:"Thiruvarur"
}
console.log(house["noofrooms"]);
console.log(house["type"]);
Why do we need bracket notation?
It is especially useful when the property name is stored in a variable.
Example:
let student = {
name: "Raksha",
age: 22
};
let key = "name";
console.log(student[key]);
Output:
Raksha
Adding Properties:
let house={
NoofMembers:4,
type:"Independent House",
noofrooms:4,
location:"Thiruvarur"
}
house.colour="Blue";
house.floors=2;
console.log(house);
Output:
{
NoofMembers: 4,
type: 'Independent House',
noofrooms: 4,
location: 'Thiruvarur',
colour: 'Blue',
floors: 2
}
Modifying Properties:
let classroom={
teacher:"Kavitha",
noofStudents:50
}
console.log(classroom);
classroom.noofStudents=70;
console.log(classroom);
Output:
Before Modification:
{ teacher: 'Kavitha', noofStudents: 50 }
After Modification:
{ teacher: 'Kavitha', noofStudents: 70 }
Deleting Properties:
// Delete
let animal={
name:"Tiger",
colour:"Orange",
gender:"Male",
isActive:true,
noofsiblings:5
}
console.log(animal);
delete animal.isActive;
console.log(animal);
Output:
Before Deletion:
{
name: 'Tiger',
colour: 'Orange',
gender: 'Male',
isActive: true,
noofsiblings: 5
}
After Deletion:
{
name: 'Tiger',
colour: 'Orange',
gender: 'Male',
noofsiblings: 5
}
Top comments (2)
Or
Symbol.Thank you for pointing that out! 😊 You're right. Object property keys can be strings or Symbols. I'll update the explanation to make it more technically complete.