JavaScript objects are collection of key-value pairs.
Key is property name and value is data stored into it property.
eg: let person ={
"name":"John",
"age:12
}
Object help us organise data. Represent real world entities.
Create Object
Object can be created using {}. You can add as many properties in object
let person = {
name: "John",
age: 25,
city: "London"
};
Accessing Object
Their are 2 ways of accessing Object:
- Dot Notation
console.log(person.name);
console.log(person.age);
2.Bracket Notation
console.log(person["name"]);
console.log(person["city"]);
Adding new properties
person.country = "UK";
console.log(person);
Delete Properties
You remove properties using delete keyword:
delete person.city;
**Looping through Object**
for(let key in objects){
console.log(key,objects[key])
}
console.log(person);
let arrPerson = Object.values(person)
Top comments (0)