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

JS Arithmetic

Every calculator you've ever used, from a simple pocket one to the calculator app on your phone, understands the same basic operations, add, subtract, multiply, divide. JavaScript's arithmetic operators give your code that exact same calculator power, plus a few extras like exponents and remainders, letting scripts compute totals, percentages, and countdowns on the fly. Whether it's calculating a reading time estimate or a quiz score on cookiescursor.com, arithmetic operators are doing the math behind the scenes.
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. #}
🔒

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.