Definition:
A JavaScript object is a collection of key-value pairs used to store related data and functionality.
const person = {
firstName: "Alagu",
lastName: "Selvan",
age: 21,
isEmployee: false
};
console.log(person);
Adding Properties:
const person = {
name: "Alagu"
};
person.age = 21;
console.log(person);
Updating Properties:
person.age = 30;
Deleting Properties:
delete person.age;
OBJECT METHODS:
Objects can also contain functions
const person = {
name: "Alagu",
ak() {
console.log("Hello ak");
}
};
person.ak();
This Keyword:
Inside an object, this refers to the current object.
const person = {
name: "Alagu",
ak() {
console.log("Hello " + this.name);
}
};
person.ak();
NESTED OBJECTS:
Objects can contain other objects.
const employee = {
name: "Alagu",
address: {
city: "Chennai",
state: "Tamil Nadu"
}
};
console.log(employee.address.city);
Top comments (0)