What objects are and why they are needed
ऑब्जेक्ट एक ऐसा डेटा स्ट्रक्चर है जिसमें की (key) और वैल्यू (value) की जोड़ी होती है।
इसका उपयोग तब होता है जब हमें किसी चीज़ के अलग-अलग गुण (properties) को संगठित तरीके से रखना हो।
------------------------------------------------
let person = {
name: "amit",
age: 25,
city: "delhi"
};
हाँ person एक ऑब्जेक्ट है।
name, age, city → ये keys हैं |
"amit", 25, "delhi" → ये उनके values हैं |
- अगर हमें किसी व्यक्ति की जानकारी रखना है तो ऑब्जेक्ट इसे साफ़ और समझने योग्य बनाता है।
----------------------------------------------------
Creating objects
Object Literals Method
-----------------------------------------------------
let person = { name: "amit", age: 25, city: "delhi" };
----------------------------------------------------------
Accessing properties (dot notation and bracket notation)
dot notation:
console.log(person.name); // amit
console.log(person.age); // 25
bracket notation:
console.log(person["city"]); // delhi
Updating object properties
person.age = 26;
person["city"] = "mumbai";
console.log(person);
// { name: 'amit', age: 26, city: 'mumbai' }
Adding and deleting properties
Adding:
person.country = "bharat";
console.log(person);
// { name: 'amit', age: 26, city: 'mumbai', country: 'bharat' }
deleting :
delete person.city;
console.log(person);
// { name: 'amit', age: 26, country: 'bharat' }
Looping through object keys
for (let key in person) {
console.log(key + ": " + person[key]);
}
/* Output:
name: amit
age: 26
country: bharat
*/
