Understanding Object in Javascript
Beginners Guide to know what is Object in javascript and how it works

What are objects and why they are needed?
In javascript an object is a collection of key-value pairs used to store information and data. Consider Aadhar Card, where you have your all data stored. Instead storing in seperate variables, we store this in one object.
let person = {
name: "Rahul",
age: 25,
city: "Mumbai"
};
Here,
Key:name,age,city
values:"Rahul",25,"Mumbai"
Creating Objects:
//With curly
let person = {
name: "Amit",
age: 30,
city: "Pune"
};
console.log(person);
//Output
{name: "Amit", age: 30, city: "Pune"}
//Each property is separated using a comma.
//Object Constructor
let person = new Object()
person.name = "Rahul"
person.age = 25
person.city = "Mumbai"
console.log(person)
//Function Construction
function Person(name, age, city) {
this.name = name
this.age = age
this.city = city
}
let p1 = new Person("Rahul", 25, "Mumbai")
let p2 = new Person("Anita", 22, "Pune")
console.log(p1)
console.log(p2)
//Using Createlet personPrototype = {
greet: function() {
console.log("Hello!")
}
}
let person = Object.create(personPrototype)
person.name = "Rahul"
person.greet() // Hello!
Accessing Object Properties with Dot Notation and Bracket Notation
let person = {
name: "Amit",
age: 30,
city: "Pune"
};
console.log(person);
//1.Dot Notation
console.log(person.name);
console.log(person.age);
//2.Bracket Notation
console.log(person["city"]);
Updating object Properties
let key = "name";
console.log(person[key]);
//Output:{name: "Amit", age: 31, city: "Pune"}
Adding New Properties
person.country = "India";
console.log(person);
//Output:{name: "Amit", age: 31, city: "Pune", country: "India"}
Deleting Properties
delete person.city;
console.log(person);
//Output:{name: "Amit", age: 31, country: "India"}
Looping Through Object Keys
for (let key in person) {
console.log(key, person[key]);
}
//name Amit
//age 31
//country India
Hope you get the object glance.
Thank You!!!
1 views
