JavaScript Data Types Explained
JavaScript Data Types అంటే ఏమిటి?
JavaScript లో variable లో store చేసే value ఏ రకానికి చెందినదో దాని data type అంటారు. ఉదాహరణకు ఒక variable లో name ఉంటే అది string, age ఉంటే number, ఒక పని పూర్తయిందా లేదా అని చూపిస్తే boolean అవుతుంది.
JavaScript లోని ముఖ్యమైన Data Types
- String: Text లేదా characters ను store చేస్తుంది. ఉదాహరణ: “Ravi”
- Number: Integers మరియు decimal values ను store చేస్తుంది. ఉదాహరణ: 25, 99.5
- Boolean: true లేదా false అనే రెండు values లో ఒకదాన్ని store చేస్తుంది.
- Undefined: Variable declare చేసినా value assign చేయనప్పుడు వచ్చే type.
- Null: Value లేదని developer ఉద్దేశపూర్వకంగా సూచించడానికి ఉపయోగించే value.
- BigInt: చాలా పెద్ద integer values కోసం ఉపయోగించే data type.
- Symbol: Unique identifiers కోసం ఉపయోగించే ప్రత్యేక data type.
- Object: Related values ను key-value pairs రూపంలో store చేస్తుంది. Arrays మరియు functions కూడా JavaScript లో object category కి సంబంధించినవే.
String Data Type
String అంటే text data. Strings ను single quotes, double quotes లేదా backticks లో రాయవచ్చు. ఉదాహరణలు: ‘Hello’, “Hello”, `Hello`.
Number Data Type
JavaScript లో whole numbers మరియు decimal numbers రెండూ సాధారణంగా number type గా ఉంటాయి. ఉదాహరణకు 10 మరియు 10.5 రెండూ number values.
Boolean Data Type
Boolean data type లో true లేదా false మాత్రమే ఉంటాయి. Login అయ్యిందా, item available లో ఉందా వంటి పరిస్థితులను సూచించడానికి ఇది ఉపయోగపడుతుంది.
Undefined మరియు Null మధ్య తేడా
undefined అంటే variable కి ఇంకా value ఇవ్వలేదు. null అంటే value ఉండకూడదని లేదా ప్రస్తుతం value లేదని developer ఉద్దేశపూర్వకంగా పెట్టాడు.
typeof Operator
ఒక value యొక్క data type తెలుసుకోవడానికి typeof operator ఉపయోగిస్తాం. ఉదాహరణకు typeof 25 result గా “number” వస్తుంది.
గమనించండి: JavaScript లో typeof null result గా “object” ఇస్తుంది. ఇది JavaScript లో ఉన్న పాత, ప్రత్యేకమైన behavior. అందువల్ల null ఉందా అని తనిఖీ చేసేటప్పుడు సాధారణంగా strict equality ఉపయోగిస్తారు.
Simple Explanation
Data type అనేది variable లో ఉన్న value ఏ రకమైనదో చెప్పే గుర్తింపు. ఒక box లో పుస్తకం ఉంటే అది text కాదు; ఒక box లో సంఖ్య ఉంటే అది number. అదే విధంగా JavaScript కూడా ప్రతి value ను దాని స్వభావం ఆధారంగా గుర్తిస్తుంది.
- Text కోసం string ఉపయోగించండి.
- సంఖ్యల కోసం number ఉపయోగించండి.
- అవును లేదా కాదు అనే పరిస్థితుల కోసం boolean ఉపయోగించండి.
- Value ఇవ్వని variable కు undefined వస్తుంది.
- Value లేదని ఉద్దేశపూర్వకంగా చూపించడానికి null ఉపయోగించండి.
- చాలా values లేదా structured data కోసం object లేదా array ఉపయోగించండి.
Real-Life Example
ఒక online shopping product ను ఊహించుకోండి. Product name “Keyboard” ఒక string. Product price 1500 ఒక number. Product stock లో ఉందా లేదా అనే సమాచారం true లేదా false అనే boolean. Product వివరాలను name, price, stock వంటి fields తో కలిపి ఉంచితే అది object అవుతుంది.
Code Examples
Basic Data Types
const studentName = "Anitha";
const age = 20;
const isLearning = true;
console.log(studentName);
console.log(age);
console.log(isLearning);
ఇక్కడ studentName లో text కాబట్టి అది string. age లో సంఖ్య కాబట్టి number. isLearning లో true కాబట్టి boolean. const తో declare చేసిన variables ను ఈ code లో తిరిగి assign చేయడం లేదు.
Anitha 20 true
typeof తో Data Type తెలుసుకోవడం
let value = "JavaScript";
console.log(typeof value);
value = 42;
console.log(typeof value);
value = false;
console.log(typeof value);
let missingValue;
console.log(typeof missingValue);
console.log(typeof null);
typeof operator value యొక్క type ను string రూపంలో ఇస్తుంది. ఒకే variable కి వేర్వేరు values assign చేసినప్పుడు ప్రతి value యొక్క type మారవచ్చు. చివరలో typeof null కు JavaScript ప్రత్యేక behavior వల్ల “object” వస్తుంది.
string number boolean undefined object
Array మరియు Object Values
const skills = ["HTML", "CSS", "JavaScript"];
const student = {
name: "Ravi",
completed: true
};
console.log(JSON.stringify(skills));
console.log(JSON.stringify(student));
skills ఒక array, అందులో అనేక values ఉన్నాయి. student ఒక object, అందులో key-value pairs ఉన్నాయి. Output ను predictable గా చూపించడానికి JSON.stringify ఉపయోగించాం.
["HTML","CSS","JavaScript"]
{"name":"Ravi","completed":true}
Line-by-Line Explanation
ఈ line లో studentName అనే constant variable create చేసి, దానికి “Anitha” అనే string value ఇచ్చాం.
age variable లో 20 అనే number value store చేశాం.
isLearning variable లో true అనే boolean value store చేశాం. ఇది student ప్రస్తుతం నేర్చుకుంటున్నాడని సూచిస్తుంది.
ఈ ఖాళీ line code ను చదవడానికి సులభంగా ఉండేందుకు మాత్రమే ఉంచబడింది; program పై దాని ప్రభావం ఉండదు.
studentName లో ఉన్న string value ను console లో చూపిస్తుంది.
age లో ఉన్న number value ను console లో చూపిస్తుంది.
isLearning లో ఉన్న boolean value అయిన true ను console లో చూపిస్తుంది.
Try It
Try this JavaScript code:
const productName = "Notebook";
const quantity = 3;
const price = 45;
const inStock = true;
console.log(productName);
console.log(quantity);
console.log(price);
console.log(inStock);
Practice
Practice:
పై code లో total variable ను మార్చండి. quantity మరియు price ను multiply చేసి మొత్తం ధరను calculate చేయండి. తరువాత product name, quantity type, price type, stock type మరియు total ను console లో చూపించండి.
Hint:
const total = quantity * price; వంటి expression ఉపయోగించండి. Quantity మరియు price రెండూ numbers కాబట్టి multiplication result కూడా number అవుతుంది.
Common Mistakes
String value ను quotes లేకుండా రాయడం, ఉదాహరణకు const name = Ravi;.
Text values ను quotes లో రాయాలి: const name = “Ravi”;.
“25” మరియు 25 ఒకే data type అని అనుకోవడం.
“25” string, కానీ 25 number. Quotes ఉన్నాయా లేదా అనేది గమనించండి.
true లేదా false ను quotes లో రాసి boolean అని అనుకోవడం.
true మరియు false ను quotes లేకుండా రాస్తేనే boolean అవుతాయి. “true” మాత్రం string.
undefined మరియు null ఒకటే అని భావించడం.
Value assign చేయకపోతే సాధారణంగా undefined వస్తుంది. Value లేదని developer ఉద్దేశపూర్వకంగా చూపించడానికి null ఉపయోగిస్తారు.
typeof null తప్పకుండా “null” ఇస్తుందని అనుకోవడం.
JavaScript లో typeof null result “object” వస్తుంది. ఇది language యొక్క పాత ప్రత్యేక behavior.
Interview Questions
JavaScript లో data type అంటే ఏమిటి?
Answer:
Variable లో store చేసిన value ఏ రకానికి చెందినదో data type అంటారు. ఉదాహరణకు text కు string, సంఖ్యకు number, true లేదా false కు boolean ఉపయోగిస్తారు.
JavaScript లో primitive data types ఏవి?
Answer:
String, number, boolean, undefined, null, bigint మరియు symbol primitive data types. Object primitive type కాదు. Primitive మరియు reference types గురించి తరువాతి lesson లో మరింతగా నేర్చుకుంటాం.
typeof operator ఉపయోగం ఏమిటి?
Answer:
ఒక value లేదా expression యొక్క data type ను తెలుసుకోవడానికి typeof operator ఉపయోగిస్తారు. ఉదాహరణకు typeof 10 result గా “number” ఇస్తుంది.
undefined మరియు null మధ్య తేడా ఏమిటి?
Answer:
Variable declare చేసి value assign చేయకపోతే undefined వస్తుంది. Value లేదని developer ఉద్దేశపూర్వకంగా సూచిస్తే null ఉపయోగిస్తారు.
typeof null ఎందుకు “object” ఇస్తుంది?
Answer:
ఇది JavaScript లోని historical behavior. JavaScript ప్రారంభ దశలో ఏర్పడిన compatibility కారణాల వల్ల typeof null result “object” గా వస్తుంది.
Exam Questions
క్రింది values యొక్క data types రాయండి: “Hello”, 100, false.
Answer:
“Hello” – string, 100 – number, false – boolean.
Value assign చేయకుండా declare చేసిన variable సాధారణంగా ఏ value కలిగి ఉంటుంది?
Answer:
అటువంటి variable value undefined గా ఉంటుంది.
typeof “50” మరియు typeof 50 results ఏమిటి?
Answer:
typeof “50” result “string”. typeof 50 result “number”.
Boolean data type లో ఉండే రెండు values ఏవి?
Answer:
Boolean data type లో true మరియు false అనే రెండు values ఉంటాయి.
Quick Revision
-
Data type అనేది variable లోని value ఏ రకమైనదో తెలియజేస్తుంది.
-
Text కోసం string, సంఖ్యల కోసం number, true లేదా false కోసం boolean ఉపయోగిస్తారు.
-
Value assign చేయని variable కు undefined వస్తుంది.
-
Value లేదని ఉద్దేశపూర్వకంగా సూచించడానికి null ఉపయోగిస్తారు.
-
చాలా పెద్ద integer values కోసం BigInt, unique identifiers కోసం Symbol ఉపయోగిస్తారు.
-
Related values ను object లేదా array లో store చేయవచ్చు.
-
Data type తెలుసుకోవడానికి typeof operator ఉపయోగిస్తారు.
-
JavaScript లో typeof null result “object” వస్తుంది.
