JavaScript Operators Practice
JavaScript Operators Practice
ఈ lesson లో JavaScript operators మరియు type conversion ను practice చేస్తాము. Operators values పై calculations, comparisons మరియు logical decisions చేయడానికి ఉపయోగపడతాయి.
ముఖ్యమైన Operators
- Arithmetic operators: +, -, *, /, %
- Assignment operator: =
- Comparison operators: ==, ===, !=, !==, >, <, >=, <=
- Logical operators: &&, ||, !
Type Conversion
కొన్ని values string రూపంలో ఉంటాయి. వాటిని calculations కోసం number గా మార్చాలి. Number() string ను number గా మార్చడానికి ఉపయోగిస్తాము. String() value ను string గా మార్చడానికి ఉపయోగిస్తాము.
ఉదాహరణకు, “100” ఒక string. Number(“100”) ఉపయోగిస్తే అది number 100 గా మారుతుంది. Strict comparison లో value తో పాటు data type కూడా check చేయబడుతుంది. అందువల్ల == మరియు === ఫలితాలు వేర్వేరుగా ఉండవచ్చు.
Operators ఉపయోగించే విధానం
- మొదట values ను variables లో store చేయండి.
- అవసరమైతే type conversion చేయండి.
- తర్వాత arithmetic లేదా comparison operator ఉపయోగించండి.
- చివరగా result ను console.log() తో పరిశీలించండి.
Simple Explanation
Operators అనేవి values మధ్య పని చేసే symbols. ఉదాహరణకు, + రెండు numbers ను కలుపుతుంది, * multiplication చేస్తుంది మరియు >= ఒక value మరొక value కంటే greater than లేదా equal గా ఉందా అని check చేస్తుంది.
Type conversion అంటే ఒక data type ను మరొక data type గా మార్చడం. Number(“25”) string value ను number గా మార్చుతుంది. User input లేదా API data తరచుగా strings రూపంలో రావచ్చు కాబట్టి calculations ముందు conversion అవసరం కావచ్చు.
సాధారణంగా comparison కోసం === మరియు !== ఉపయోగించడం మంచిది, ఎందుకంటే ఇవి value మరియు type రెండింటినీ check చేస్తాయి.
Real-Life Example
ఒక shopping bill లో product price మరియు quantity values server నుంచి strings గా వచ్చాయని అనుకోండి. Price “250”, quantity “2” గా ఉంటే వాటిని Number() తో numbers గా మార్చి multiplication చేయాలి. అప్పుడు total bill ను సరిగ్గా calculate చేయవచ్చు.
Code Examples
Type Conversion తో Total Bill
const price = "100";
const quantity = 2;
const total = Number(price) * quantity;
console.log(total);
console.log(total >= 200);
price విలువ string గా ఉంది. Number(price) ద్వారా దాన్ని number గా మార్చి quantity తో multiply చేస్తున్నాము. చివరి comparison లో total 200 కంటే greater than లేదా equal గా ఉందా అని check చేస్తున్నాము.
200 true
== మరియు === మధ్య తేడా
const textNumber = "25";
const number = 25;
console.log(textNumber == number);
console.log(textNumber === number);
== అవసరమైతే type conversion చేసి values ను compare చేస్తుంది. అందువల్ల string 25 మరియు number 25 సమానంగా కనిపిస్తాయి. === value మరియు type రెండింటినీ compare చేస్తుంది కాబట్టి రెండవ result false అవుతుంది.
true false
Arithmetic మరియు Logical Operators
const completedLessons = 6;
const totalLessons = 10;
const hasAccess = true;
const progress = completedLessons / totalLessons * 100;
const canContinue = progress >= 50 && hasAccess;
console.log(progress);
console.log(canContinue);
మొదట completed lessons శాతాన్ని calculate చేస్తున్నాము. తర్వాత progress 50 లేదా అంతకంటే ఎక్కువగా ఉందా మరియు user కు access ఉందా అనే రెండు conditions ను && operator తో check చేస్తున్నాము.
60 true
Line-by-Line Explanation
price variable లో 100 అనే string value ను store చేస్తున్నాము. Quotes ఉన్నందున ఇది number కాదు, string.
quantity variable లో number value 2 ను store చేస్తున్నాము.
Number(price) ద్వారా price string ను number గా మార్చుతున్నాము. తర్వాత * operator తో quantity ను multiply చేసి total ను calculate చేస్తున్నాము.
ఈ blank line code ను readable గా ఉంచడానికి ఉపయోగించబడింది. దీనివల్ల program execution పై ఎలాంటి ప్రభావం ఉండదు.
Calculated total value ను console లో display చేస్తుంది.
>= operator total value 200 కంటే greater than లేదా equal గా ఉందా అని check చేస్తుంది. Result boolean value అవుతుంది.
Try It
Try this JavaScript code:
const itemPrice = "80";
const itemQuantity = "3";
const price = Number(itemPrice);
const quantity = Number(itemQuantity);
const total = price * quantity;
console.log(total);
Practice
Practice: firstScore మరియు bonus string values ను numbers గా convert చేసి, secondScore తో కలిపి finalScore calculate చేయండి. తర్వాత finalScore కనీసం 75 ఉందా అని comparison operator తో check చేయండి.
Hint: Number() ను string values పై ఉపయోగించండి. Addition కోసం + మరియు condition కోసం >= ఉపయోగించండి.
Common Mistakes
String numbers ను conversion లేకుండా arithmetic calculation చేయడం. ఉదాహరణకు “10” + 5 ను number addition అని అనుకోవడం.
Calculation ముందు string ను Number() తో convert చేయండి. Number(“10”) + 5 result 15 అవుతుంది.
Value మాత్రమే compare చేయడానికి ప్రతిసారి == ఉపయోగించడం.
సాధారణంగా predictable comparison కోసం === ఉపయోగించండి. ఇది value మరియు data type రెండింటినీ check చేస్తుంది.
Comparison operator result number వస్తుందని అనుకోవడం.
>, <, === వంటి operators సాధారణంగా true లేదా false అనే boolean result ఇస్తాయి.
Variable లో string value ఉందా లేదా number value ఉందా అని గుర్తించకపోవడం.
Quotes ఉన్న values strings అని గుర్తుంచుకోండి. అవసరమైనప్పుడు Number() లేదా String() తో explicit conversion చేయండి.
Interview Questions
JavaScript లో == మరియు === మధ్య తేడా ఏమిటి?
Answer:
== comparison ముందు అవసరమైతే type conversion చేయవచ్చు. === value మరియు data type రెండింటినీ compare చేస్తుంది. అందువల్ల reliable comparisons కోసం సాధారణంగా === ఉపయోగిస్తారు.
Number() function ఎందుకు ఉపయోగిస్తారు?
Answer:
String లేదా ఇతర convertible value ను number గా మార్చడానికి Number() ఉపయోగిస్తారు. Calculations చేసే ముందు numeric data అవసరమైనప్పుడు ఇది ఉపయోగకరం.
Logical && operator ఎలా పని చేస్తుంది?
Answer:
&& operator రెండు conditions true అయినప్పుడు మాత్రమే true result ఇస్తుంది. ఒక condition false అయితే మొత్తం result false అవుతుంది.
Comparison operators ఏ type result ఇస్తాయి?
Answer:
Comparison operators సాధారణంగా boolean result ఇస్తాయి. అంటే result true లేదా false అవుతుంది.
Exam Questions
ఈ code output ఏమిటి? const a = “10”; const b = 5; console.log(Number(a) + b);
Answer:
15
ఈ code లో రెండవ output ఏమిటి? const value = “20”; console.log(value == 20); console.log(value === 20);
Answer:
false
ఒక value 100 కంటే తక్కువగా ఉందా అని check చేయడానికి ఏ operator ఉపయోగిస్తారు?
Answer:
< operator ఉపయోగిస్తారు. ఉదాహరణకు value < 100.
రెండు conditions రెండూ true అయినప్పుడు మాత్రమే true result పొందడానికి ఏ logical operator ఉపయోగిస్తారు?
Answer:
&& operator ఉపయోగిస్తారు.
Quick Revision
- +, –, *, / arithmetic calculations కోసం ఉపయోగిస్తారు.
- Number() string value ను number గా మార్చుతుంది.
- String() value ను string గా మార్చుతుంది.
- === value మరియు type రెండింటినీ compare చేస్తుంది.
- == comparison ముందు type conversion చేయవచ్చు.
- >, <, >=, <= comparison operators boolean result ఇస్తాయి.
- && రెండు conditions true అయినప్పుడు true ఇస్తుంది.
- Calculations ముందు values యొక్క data type ను పరిశీలించండి.
