JavaScript Object Basics
JavaScript Object Basics
JavaScript లో Object అనేది related data ను ఒకే structure లో నిల్వ చేయడానికి ఉపయోగించే data type. Object లో data ను key-value pairs రూపంలో రాస్తాము.
Object Syntax
Object ను curly braces {} మధ్య రాస్తాము. ప్రతి property కి ఒక key మరియు value ఉంటాయి.
ఉదాహరణ: name: “Ravi” లో name అనేది key, “Ravi” అనేది value.
Object Creating
const లేదా let తో object variable ను create చేయవచ్చు. Object లో string, number, boolean, array లేదా మరొక object వంటి values ఉంచవచ్చు.
Properties Access చేయడం
Object property ని access చేయడానికి రెండు సాధారణ పద్ధతులు ఉన్నాయి:
- Dot notation: student.name
- Bracket notation: student[“name”]
Key పేరు ఒక variable లో ఉన్నప్పుడు లేదా key లో spaces ఉన్నప్పుడు bracket notation ఉపయోగపడుతుంది.
Property Value మార్చడం
Object property కి కొత్త value assign చేస్తే ఆ property value update అవుతుంది. ఉదాహరణకు student.age = 21 అని రాస్తే age value 21 అవుతుంది.
Property తొలగించడం
delete keyword తో object property ని తొలగించవచ్చు. తొలగించిన property ని access చేస్తే సాధారణంగా undefined వస్తుంది.
ఈ lesson లో objects యొక్క basic structure, properties, property access, update మరియు delete operations నేర్చుకుంటాము. Object methods గురించి తరువాతి lesson లో తెలుసుకుంటాము.
Simple Explanation
ఒక వ్యక్తికి పేరు, వయస్సు, నగరం వంటి అనేక వివరాలు ఉంటాయి. వీటిని వేర్వేరు variables గా కాకుండా ఒకే object లో ఉంచవచ్చు.
student object లో name, age, isLearning వంటి properties ఉండవచ్చు. Property value చదవడానికి dot notation లేదా bracket notation ఉపయోగిస్తాము.
సులభంగా గుర్తుంచుకోండి: Object అంటే related information ను labels తో కలిపి ఉంచే container.
Real-Life Example
ఒక school student registration form ను ఊహించండి. అందులో student name, roll number, class మరియు phone number వంటి details ఉంటాయి. ఇవన్నీ ఒకే student object లో ఇలా నిర్వహించవచ్చు: name, rollNumber, className మరియు phoneNumber.
అలాగే online shopping లో ఒక product object లో product name, price, category మరియు stock status వంటి details నిల్వ చేయవచ్చు. దీని వల్ల related data ను సులభంగా access మరియు update చేయవచ్చు.
Code Examples
Object Create, Access, Update మరియు Delete
const student = {
name: "Anu",
age: 20,
isLearning: true
};
console.log(student.name);
console.log(student["age"]);
student.age = 21;
console.log(student.age);
delete student.isLearning;
console.log(student.isLearning);
ఇక్కడ student అనే object లో మూడు properties ఉన్నాయి. student.name ద్వారా dot notation ఉపయోగించాము. student[“age”] ద్వారా bracket notation ఉపయోగించాము. తరువాత age value ను 20 నుంచి 21 కి మార్చాము. చివరగా delete keyword తో isLearning property ని తొలగించాము.
Anu 20 21 undefined
Object లో Different Data Types
const product = {
name: "Notebook",
price: 50,
available: true,
colors: ["red", "blue"]
};
console.log(product.name);
console.log(product.price);
console.log(product.available);
console.log(product.colors[0]);
ఒక object లో వివిధ data types values గా ఉండవచ్చు. ఇక్కడ name string, price number, available boolean మరియు colors array. Object లోని array value ను access చేయడానికి property access తరువాత array index ఉపయోగించాము.
Notebook 50 true red
Line-by-Line Explanation
student అనే constant variable ను create చేసి, object ప్రారంభిస్తున్నాము. Opening curly brace object ప్రారంభాన్ని సూచిస్తుంది.
ఇది object లోని name property. దీని value string అయిన “Anu”.
ఇక్కడ age property కి number value 20 ఇచ్చాము.
isLearning property కి boolean value true ఇచ్చాము. చివరి property తరువాత comma అవసరం లేదు.
Curly brace object ను ముగిస్తుంది. Semicolon statement ముగిసిందని సూచిస్తుంది.
ఇది code readability కోసం ఉంచిన blank line. దీనివల్ల program execution పై ప్రభావం ఉండదు.
Dot notation ఉపయోగించి student object లోని name property value ను console లో చూపిస్తుంది.
Bracket notation ఉపయోగించి age property value ను access చేసి console లో చూపిస్తుంది.
ఇది code ను logical sections గా విడగొట్టడానికి ఉపయోగించిన blank line.
ఇప్పటికే ఉన్న age property కి కొత్త value 21 assign చేస్తున్నాము. అందువల్ల పాత value 20 update అవుతుంది.
Update చేసిన age value 21 ను console లో చూపిస్తుంది.
ఇది readability కోసం ఉంచిన blank line.
delete keyword ఉపయోగించి student object నుంచి isLearning property ని తొలగిస్తుంది.
isLearning property తొలగించబడినందువల్ల దాన్ని access చేసినప్పుడు undefined వస్తుంది.
Try It
Try this JavaScript code:
const product = {
name: "Notebook",
price: 50,
inStock: true
};
console.log(product.name);
console.log(product.price);
Practice
Practice: employee object లోని name మరియు city values ను console లో print చేయండి. తరువాత city value ను “Vijayawada” గా update చేసి, updated city ని మళ్లీ print చేయండి.
Hint: Name కోసం dot notation ఉపయోగించండి. City మార్చడానికి employee.city కి కొత్త string value assign చేయండి.
Common Mistakes
Object property మరియు value మధ్య colon : బదులు equals = ఉపయోగించడం.
Object లో property రాయడానికి key: value syntax ఉపయోగించాలి. ఉదాహరణ: name: “Ravi”.
ఒకే object లోని properties మధ్య comma పెట్టడం మర్చిపోవడం.
చివరి property తప్ప మిగిలిన ప్రతి property తరువాత comma పెట్టాలి.
Dot notation లో property పేరు quotes తో రాయడం, ఉదాహరణకు student.”name”.
Dot notation లో quotes అవసరం లేదు: student.name. Quotes అవసరమైతే bracket notation అయిన student[“name”] ఉపయోగించండి.
const object లోని property value మార్చలేమని అనుకోవడం.
const object variable కి కొత్త object assign చేయలేము. కానీ ఆ object లోని existing property values ను మార్చవచ్చు.
లేని property ని access చేసినప్పుడు error తప్పనిసరిగా వస్తుందని అనుకోవడం.
సాధారణంగా లేని object property ని access చేస్తే undefined value వస్తుంది.
Interview Questions
JavaScript object అంటే ఏమిటి?
Answer:
JavaScript object అనేది related data ను key-value pairs రూపంలో నిల్వ చేసే data structure. ఉదాహరణకు student object లో name, age మరియు course వంటి properties ఉండవచ్చు.
Dot notation మరియు bracket notation మధ్య తేడా ఏమిటి?
Answer:
Dot notation లో object.key రూపం ఉపయోగిస్తాము. Bracket notation లో object[“key”] రూపం ఉపయోగిస్తాము. Dynamic key లేదా spaces ఉన్న key కోసం bracket notation ఉపయోగకరం.
const తో create చేసిన object property ని update చేయవచ్చా?
Answer:
అవును. const variable కి కొత్త object assign చేయలేము, కానీ అదే object లోని properties ను update చేయవచ్చు.
Object లో property ని ఎలా delete చేస్తారు?
Answer:
delete object.property syntax ఉపయోగించి property ని తొలగించవచ్చు.
Exam Questions
క్రింది object లో city value ను ఎలా access చేస్తారు? const user = { name: “Sita”, city: “Guntur” };
Answer:
user.city లేదా user[“city”] ఉపయోగించవచ్చు.
క్రింది statement తరువాత output ఏమిటి? const item = { price: 100 }; item.price = 150; console.log(item.price);
Answer:
Output 150. ఎందుకంటే price property value 100 నుంచి 150 కి update అయింది.
Object లో key-value pair syntax ను రాయండి.
Answer:
సరైన syntax: key: value. ఉదాహరణ: title: “JavaScript”.
Quick Revision
- Object related data ను key-value pairs రూపంలో నిల్వ చేస్తుంది.
- Object ను curly braces {} మధ్య create చేస్తాము.
- Property access కోసం dot notation మరియు bracket notation ఉపయోగిస్తాము.
- Existing property value ను కొత్త value assign చేసి update చేయవచ్చు.
- delete keyword తో object property ని తొలగించవచ్చు.
- లేని property ని access చేస్తే సాధారణంగా undefined వస్తుంది.
- Object లో string, number, boolean, array వంటి values ఉంచవచ్చు.
