Home » Tech Tutorials » Web Development » JavaScript » JavaScript Arithmetic Operators

JavaScript Arithmetic Operators

JavaScript Arithmetic Operators

JavaScript లో numbers పై calculations చేయడానికి Arithmetic Operators ఉపయోగిస్తాము. Addition కోసం +, subtraction కోసం –, multiplication కోసం *, division కోసం /, remainder కోసం % మరియు power కోసం ** ఉపయోగిస్తారు.

ప్రధాన Arithmetic Operators

  • + : రెండు values ను కలుపుతుంది.
  • – : ఒక value నుంచి మరొక value ను తీసివేస్తుంది.
  • * : multiplication చేస్తుంది.
  • / : division చేస్తుంది.
  • % : division తర్వాత మిగిలే remainder ను ఇస్తుంది.
  • ** : power లేదా exponent calculation చేస్తుంది.
  • ++ : value ను ఒకటి పెంచుతుంది.
  • — : value ను ఒకటి తగ్గిస్తుంది.

Operator Precedence

ఒక expression లో చాలా operators ఉన్నప్పుడు JavaScript ముందుగా multiplication, division మరియు remainder ను calculate చేస్తుంది. తర్వాత addition మరియు subtraction ను calculate చేస్తుంది. ఈ order మార్చడానికి parentheses ఉపయోగించవచ్చు.

ఉదాహరణకు 2 + 3 * 4 ఫలితం 14, ఎందుకంటే ముందుగా 3 * 4 calculate అవుతుంది. (2 + 3) * 4 ఫలితం 20, ఎందుకంటే parentheses లోని calculation ముందుగా జరుగుతుంది.

Type Conversion

కొన్నిసార్లు number లాగా కనిపించే value నిజానికి string అయి ఉండవచ్చు. ఉదాహరణకు “120” ఒక string. దానిని calculation కోసం number గా మార్చడానికి Number() ఉపయోగించవచ్చు. Number ను string గా మార్చడానికి String() ఉపయోగిస్తాము.

+ operator తో strings కలిసినప్పుడు concatenation జరుగుతుంది. ఉదాహరణకు “10” + 5 ఫలితం “105”. కానీ “10” – 5 లో JavaScript string ను number గా మార్చడానికి ప్రయత్నిస్తుంది, కాబట్టి ఫలితం 5 అవుతుంది. ఈ implicit conversion పై ఆధారపడటం కంటే Number() ద్వారా explicit conversion చేయడం స్పష్టమైన పద్ధతి.

Conversion విఫలమైతే Number() ఫలితంగా NaN వస్తుంది. NaN అంటే Not a Number.

Simple Explanation

Arithmetic operators calculator లోని buttons లాంటివి. + కలుపుతుంది, – తీసివేస్తుంది, * గుణిస్తుంది, / భాగిస్తుంది. % ద్వారా మిగిలిన remainder తెలుసుకోవచ్చు.

ఒక value string రూపంలో ఉంటే calculation ముందు దానిని Number() తో number గా మార్చండి. ఇలా చేయడం వల్ల code అర్థం చేసుకోవడం సులభంగా ఉంటుంది మరియు unexpected results తగ్గుతాయి.

Real-Life Example

ఒక shop లో ఒక product ధర 120 రూపాయలు, quantity 3 ఉంటే subtotal ను price * quantity తో calculate చేయవచ్చు. Customer కు 50 రూపాయల discount ఇస్తే final total ను subtotal – discount తో కనుక్కోవచ్చు.

Web form నుంచి price మరియు quantity సాధారణంగా text రూపంలో వస్తాయి. కాబట్టి calculation ముందు వాటిని Number() తో numbers గా మార్చడం అవసరం.

Code Examples

Basic Arithmetic Operators

const a = 10;
const b = 3;

console.log("Addition:", a + b);
console.log("Subtraction:", a - b);
console.log("Multiplication:", a * b);
console.log("Division:", a / b);
console.log("Remainder:", a % b);
console.log("Power:", a ** b);

ఈ example లో రెండు number variables పై arithmetic operations నిర్వహించాము. Division ఫలితం decimal కావచ్చు. % operator division తర్వాత మిగిలిన remainder ను ఇస్తుంది.

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Remainder: 1
Power: 1000

Explicit Type Conversion తో Total Calculation

const priceText = "120";
const quantityText = "3";

const price = Number(priceText);
const quantity = Number(quantityText);
const subtotal = price * quantity;
const discount = 50;
const total = subtotal - discount;

console.log("Total:", total);

priceText మరియు quantityText strings. Number() ఉపయోగించి వాటిని numbers గా మార్చిన తర్వాత multiplication మరియు subtraction చేశాము.

Total: 310

String Concatenation మరియు Conversion

const textNumber = "10";
const numberValue = 5;

console.log("With plus:", textNumber + numberValue);
console.log("After conversion:", Number(textNumber) + numberValue);
console.log("As text:", String(numberValue));

String మరియు number ను నేరుగా + తో కలిపితే concatenation జరుగుతుంది. Number() ఉపయోగించినప్పుడు addition arithmetic operation గా పనిచేస్తుంది. String() number ను string గా మారుస్తుంది.

With plus: 105
After conversion: 15
As text: 5

Line-by-Line Explanation

const priceText = “120”;

ఇక్కడ priceText అనే constant లో 120 string రూపంలో store చేశాము.

const quantityText = “3”;

ఇక్కడ quantity విలువను కూడా string రూపంలో store చేశాము. Form input values సాధారణంగా ఇలాగే strings గా వస్తాయి.

const price = Number(priceText);

Number() function ఉపయోగించి priceText string ను number గా మార్చి price variable లో ఉంచాము.

const quantity = Number(quantityText);

quantityText ను number గా convert చేసి quantity variable లో store చేశాము.

const subtotal = price * quantity;

Price మరియు quantity ను * operator తో multiply చేసి subtotal ను calculate చేశాము.

const discount = 50;

Discount amount గా 50 number ను discount constant లో store చేశాము.

const total = subtotal – discount;

Subtotal నుంచి discount ను – operator తో తీసివేసి final total ను calculate చేశాము.

console.log(“Total:”, total);

Final total value ను console లో display చేశాము.

Try It

Try this JavaScript code:

Example

const priceText = "250";
const quantityText = "2";
const discount = 75;

const price = Number(priceText);
const quantity = Number(quantityText);

const subtotal = price * quantity;
const total = subtotal - discount;

console.log("Subtotal:", subtotal);
console.log("Total:", total);

Try It Yourself »

Practice

Practice:

ఒక product ధర “180”, quantity “4” మరియు discount 100 ఉన్నాయి. Number() ఉపయోగించి strings ను numbers గా మార్చండి. తర్వాత subtotal మరియు discount తీసివేసిన final total ను console లో చూపించండి.

Hint:

ముందుగా itemPrice * itemQuantity తో subtotal calculate చేయండి. తర్వాత subtotal – discount ఉపయోగించండి.

Common Mistakes

“10” + 5 ను arithmetic addition అనుకుని 15 వస్తుందని అనుకోవడం.

+ operator string ను చూసినప్పుడు concatenation చేయవచ్చు. ముందుగా Number(“10”) + 5 అని రాస్తే ఫలితం 15 వస్తుంది.

Form నుంచి వచ్చిన string values ను conversion లేకుండా calculations లో ఉపయోగించడం.

Calculation ముందు values ను Number() తో explicit గా convert చేయండి.

Discount ను subtotal కు add చేయడం.

Discount తగ్గింపు కాబట్టి final amount కోసం subtotal – discount ఉపయోగించాలి.

2 + 3 * 4 లో addition ముందుగా జరుగుతుందని అనుకోవడం.

Multiplication కు higher precedence ఉంటుంది. Addition ముందుగా కావాలంటే (2 + 3) * 4 వంటి parentheses ఉపయోగించండి.

Invalid text ను number గా మార్చినప్పుడు సాధారణ number వస్తుందని అనుకోవడం.

Number(“hello”) ఫలితంగా NaN వస్తుంది. Input valid number కాదా అని check చేయడం అవసరం.

Interview Questions

JavaScript లో % operator ఏమి చేస్తుంది?

Answer:

% operator division తర్వాత మిగిలే remainder ను return చేస్తుంది. ఉదాహరణకు 10 % 3 ఫలితం 1.

“10” + 5 మరియు “10” – 5 ఫలితాలు ఎందుకు వేర్వేరుగా ఉంటాయి?

Answer:

+ operator string ఉన్నప్పుడు concatenation చేయవచ్చు, కాబట్టి ఫలితం “105”. – operator string concatenation చేయదు; JavaScript string ను number గా convert చేయడానికి ప్రయత్నిస్తుంది, కాబట్టి ఫలితం 5.

Number() function ఉపయోగం ఏమిటి?

Answer:

Number() value ను number గా convert చేయడానికి ఉపయోగిస్తారు. Conversion సాధ్యం కాకపోతే సాధారణంగా NaN వస్తుంది.

Arithmetic expression లో parentheses ఎందుకు ఉపయోగిస్తారు?

Answer:

Parentheses calculation order ను స్పష్టంగా control చేయడానికి ఉపయోగిస్తారు. Parentheses లోని expression ముందుగా evaluate అవుతుంది.

Exam Questions

17 % 5 విలువ ఎంత?

Answer:

విలువ 2, ఎందుకంటే 17 ను 5 తో భాగించినప్పుడు remainder 2.

2 + 4 * 3 మరియు (2 + 4) * 3 ఫలితాలు ఏమిటి?

Answer:

2 + 4 * 3 ఫలితం 14. (2 + 4) * 3 ఫలితం 18.

Number(“25”) + 5 ఫలితం ఏమిటి?

Answer:

ఫలితం 30, ఎందుకంటే Number(“25”) string ను number గా మారుస్తుంది.

ఒక వస్తువు ధర 80, quantity 3 మరియు discount 20 అయితే final total ను ఏ expression తో calculate చేస్తారు?

Answer:

ముందుగా subtotal కోసం 80 * 3 calculate చేసి, తర్వాత final total కోసం (80 * 3) – 20 ఉపయోగించాలి. ఫలితం 220.

Quick Revision

  • + addition లేదా string concatenation కోసం ఉపయోగించబడుతుంది.
  • –, *, / subtraction, multiplication మరియు division కోసం ఉపయోగిస్తారు.
  • % remainder ను ఇస్తుంది; ** power calculation కోసం ఉపయోగిస్తారు.
  • Multiplication, division మరియు remainder సాధారణంగా addition, subtraction కంటే ముందుగా calculate అవుతాయి.
  • Calculation ముందు string number ను Number() తో convert చేయండి.
  • String() value ను string గా మారుస్తుంది.
  • Invalid numeric text ను Number() తో convert చేస్తే NaN రావచ్చు.
  • + తో string మరియు number కలిస్తే concatenation జరగవచ్చు.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top