← Back to JavaScript Course | Chapter 2: Functions & Objects | Lesson 1 of 10

JS अंकगणित

आपने अब तक जितने भी calculators इस्तेमाल किए हैं, एक साधारण pocket calculator से लेकर आपके फोन के calculator app तक, सब एक जैसे basic operations समझते हैं, जोड़ना, घटाना, गुणा करना, भाग करना। JavaScript के arithmetic operators आपके code को बिल्कुल वही calculator power देते हैं, साथ ही exponents और remainders जैसी कुछ अतिरिक्त चीज़ें, जो scripts को totals, percentages, और countdowns तुरंत calculate करने देती हैं। चाहे cookiescursor.com पर reading time का अंदाज़ा लगाना हो या quiz score calculate करना, arithmetic operators ही पर्दे के पीछे गणित कर रहे होते हैं।
Syntax
javascript
result = a + b;  // also -, *, /, %, **
x++;
x--;

मुख्य Operators

JavaScript के पाँच बुनियादी arithmetic operators हैं: जोड़ के लिए +, घटाव के लिए -, गुणा के लिए *, भाग के लिए /, और शेषफल के लिए %, जिसे कभी-कभी modulo कहा जाता है।

Note: % operator खास तौर पर यह जाँचने में उपयोगी है कि कोई संख्या सम है या नहीं, क्योंकि evenNumber % 2 हमेशा 0 के बराबर होता है।
Warning: Division in JavaScript always returns a decimal when the numbers don't divide evenly, there's no automatic rounding.

उदाहरण: The Core Operators

javascript
// Print `10 + 3` to the console
// Print `10 + 3` to the console
console.log(10 + 3);
// Print `10 - 3` to the console
// Print `10 - 3` to the console
console.log(10 - 3);
// Print `10 * 3` to the console
// Print `10 * 3` to the console
console.log(10 * 3);
// Print `10 / 3` to the console
// Print `10 / 3` to the console
console.log(10 / 3);
// Print `10 % 3` to the console
// Print `10 % 3` to the console
console.log(10 % 3);

Exponentiation

** operator किसी संख्या को दूसरी संख्या की घात तक बढ़ाता है, और रोज़मर्रा की अधिकांश घात गणनाओं के लिए पुराने, ज़्यादा शब्दबहुल Math.pow() function की जगह लेता है।

Note: ** बाएँ से दाएँ स्वाभाविक रूप से पढ़ा जाता है, base ** exponent, जो एक-दो बार इस्तेमाल देख लेने के बाद सहज हो जाता है।
Warning: ** binds more tightly than unary minus in some cases, so use parentheses when combining it with negative numbers to avoid ambiguity.

उदाहरण: Exponentiation

javascript
console.log(2 ** 3);
console.log(Math.pow(2, 3));

Increment और Decrement

++ किसी variable के value को एक से बढ़ाता है, और -- एक से घटाता है, दोनों एक pre रूप (++x) में उपलब्ध हैं जो value को इस्तेमाल होने से पहले बदलता है, और एक post रूप (x++) में जो पहले मूल value इस्तेमाल करता है।

Note: जब अपनी अकेली line पर increment किया जाए, तो pre और post रूप एक जैसा व्यवहार करते हैं, फ़र्क केवल तब पड़ता है जब उन्हें किसी बड़ी expression के भीतर इस्तेमाल किया जाए।
Warning: Mixing post-increment into a complex expression, like array[i++] = i, can produce confusing results if you're not careful about evaluation order.

उदाहरण: Increment and Decrement

javascript
let x = 5;
console.log(++x); // 6, pre-increment
let y = 5;
console.log(y++); // 5, post-increment
console.log(y);   // 6

Operator Precedence

JavaScript गणित का मानक क्रम अपनाती है, गुणा, भाग और शेषफल जोड़ और घटाव से पहले होते हैं, और किसी विशिष्ट evaluation क्रम को लागू करने के लिए parentheses का हमेशा इस्तेमाल किया जा सकता है।

Note: जब किसी expression का इच्छित क्रम तुरंत स्पष्ट न हो, तो parentheses जोड़ दीजिए भले ही वे सख्ती से ज़रूरी न हों, यह पठनीयता बेहतर करता है।
Warning: Assuming operations run strictly left to right without considering precedence rules is a common source of incorrect arithmetic results.

उदाहरण: Operator Precedence

javascript
console.log(2 + 3 * 4);   // 14, multiplication first
console.log((2 + 3) * 4); // 20, parentheses force order

Math Object

Built-in Math object बुनियादी operators से आगे कई उपयोगी numeric functions देता है, जिनमें Math.round(), Math.max(), Math.min() और Math.sqrt() शामिल हैं, जिन्हें एक बाद के अध्याय में ज़्यादा विस्तार से बताया गया है।

Note: अपना rounding या तुलना का logic खुद लिखने की बजाय Math object के methods का सहारा लीजिए, वे अच्छी तरह परखे हुए हैं और edge cases को सही तरह संभालते हैं।
Warning: Math methods like Math.max() with no arguments return -Infinity, which can cause unexpected results if a list turns out to be empty.

उदाहरण: The Math Object

javascript
// Print `Math.round(4.7)` to the console
// Print `Math.round(4.7)` to the console
console.log(Math.round(4.7));
// Print `Math.max(3, 7, 2)` to the console
// Print `Math.max(3, 7, 2)` to the console
console.log(Math.max(3, 7, 2));
// Print `Math.min(3, 7, 2)` to the console
// Print `Math.min(3, 7, 2)` to the console
console.log(Math.min(3, 7, 2));
// Print `Math.sqrt(16)` to the console
// Print `Math.sqrt(16)` to the console
console.log(Math.sqrt(16));
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. operator precedence भूल जाना, multiplication और division addition और subtraction से पहले चलते हैं जब तक parentheses कुछ और न कहें।
  2. remainder operator % को division / से confuse करना, % बचा हुआ हिस्सा return करता है, divided result नहीं।
  3. post-increment और pre-increment को मिला देना, x++ और ++x किसी बड़े expression के अंदर इस्तेमाल होने पर अलग व्यवहार करते हैं।
चैप्टर सारांश
  • JavaScript addition, subtraction, multiplication, division, remainder, और exponentiation के लिए +, -, *, /, %, और ** support करती है।
  • ++ और -- किसी variable को exactly एक से बढ़ाते या घटाते हैं, pre और post versions expressions में अलग व्यवहार करते हुए।
  • Operator precedence standard math rules follow करता है, और built-in Math object कई अतिरिक्त numeric functions देता है।
ब्राउज़र सपोर्ट

सभी arithmetic operators और Math object core JavaScript features हैं जो हर browser में desktop से mobile तक समान रूप से supported हैं।

🔒

Chapter Quiz — Complete all 10 topics to unlock

0/10 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.