← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 2 of 26

JS का Math

अपनी programming language के अंदर built-in एक scientific calculator के बारे में सोचें, जो हमेशा उपलब्ध है, किसी अलग app की ज़रूरत नहीं, numbers round करने, सबसे बड़ी value ढूंढने, या random pick generate करने के लिए तैयार। JavaScript का built-in Math object बिल्कुल वही calculator है, आम numeric कामों के लिए ready-made methods का एक collection, ताकि आपको rounding या randomization की logic कभी शुरू से न लिखनी पड़े। किसी rounded quiz score को दिखाने से लेकर cookiescursor.com पर कोई random featured tutorial चुनने तक, Math object चुपचाप रोज़मर्रा का बहुत सारा numeric काम संभालता है।
Syntax
javascript
Math.round(number);
Math.random();
Math.max(a, b);
Math.floor(number);
Math.PI

Rounding: round, floor, और ceil

Math.round() सबसे नज़दीकी whole number तक round करता है, Math.floor() हमेशा नीचे की ओर round करता है, और Math.ceil() हमेशा ऊपर की ओर round करता है, चाहे decimal अगले whole number के कितना भी करीब क्यों न हो।

Note: जब आप कुछ ऐसा calculate कर रहे हों जैसे 'कितने पूरे groups fit होते हैं', तो Math.floor() इस्तेमाल करें, क्योंकि यह असल में जितना fit होता है उससे आगे कभी round up नहीं करता।
Warning: Math.round() ठीक .5 को ऊपर की ओर round करता है, जो negative numbers के लिए हैरान कर सकता है, जहाँ यह उम्मीद से थोड़ा अलग व्यवहार करता है।

उदाहरण: Rounding: round, floor, and ceil

javascript
// Print `Math.round(4.5)` to the console
// Print `Math.round(4.5)` to the console
console.log(Math.round(4.5));
// Print `Math.floor(4.9)` to the console
// Print `Math.floor(4.9)` to the console
console.log(Math.floor(4.9));
// Print `Math.ceil(4.1)` to the console
// Print `Math.ceil(4.1)` to the console
console.log(Math.ceil(4.1));

abs, max, और min

Math.abs() किसी number की absolute value return करता है, जो हमेशा positive होती है, Math.max() values के एक सेट में से सबसे बड़ी value return करता है, और Math.min() सबसे छोटी।

तीनों ही किसी भी संख्या में arguments स्वीकार करते हैं और अक्सर array contents पर काम करने के लिए spread operator के साथ मिलाकर इस्तेमाल होते हैं।

Note: किसी array के साथ spread operator इस्तेमाल करें, जैसे Math.max(...scores), ताकि numbers की एक array से सीधे maximum value मिल जाए।
Warning: बिना किसी argument के कॉल किए जाने पर Math.max() और Math.min() क्रमशः -Infinity और Infinity return करते हैं, जो किसी खाली list पर comparisons को चुपचाप तोड़ सकता है।

उदाहरण: abs, max, and min

javascript
// Print `Math.abs(-5)` to the console
// Print `Math.abs(-5)` to the console
console.log(Math.abs(-5));
// 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));

Math.random()

Math.random() 0 (inclusive) और 1 (exclusive) के बीच एक random decimal number return करता है, जिसे आमतौर पर किसी खास range में एक random whole number बनाने के लिए scale और round किया जाता है।

Note: आम formula Math.floor(Math.random() * (max - min + 1)) + min भरोसेमंद तरीके से min और max के बीच, दोनों शामिल करते हुए, एक random whole number generate करता है।
Warning: Math.random() cryptographically secure नहीं है, इसे किसी भी security sensitive काम के लिए इस्तेमाल न करें, जैसे tokens या passwords generate करना।

उदाहरण: Math.random()

javascript
console.log(Math.random()); // decimal between 0 and 1
const diceRoll = Math.floor(Math.random() * 6) + 1;
console.log(diceRoll);

pow और sqrt

Math.pow(base, exponent) किसी number को एक power तक raise करता है, जो ** operator के बराबर ही काम करता है, और Math.sqrt() किसी number का square root निकालता है।

किसी negative number का Math.sqrt() NaN return करता है, क्योंकि JavaScript का Math object सिर्फ real numbers के साथ काम करता है।

Note: modern code में, ज़्यादा concise syntax की वजह से आमतौर पर Math.pow() से ज़्यादा ** operator को prefer किया जाता है, हालांकि दोनों एक जैसे ही results देते हैं।
Warning: किसी negative number का Math.sqrt() error नहीं बल्कि NaN return करता है, इसलिए अगर negatives possible हों तो कॉल करने से पहले value का sign जांच लेना सही रहता है।

उदाहरण: pow and sqrt

javascript
console.log(Math.pow(2, 3));
console.log(Math.sqrt(16));
console.log(Math.sqrt(-4)); // NaN

Math.PI और अन्य Constants

Math.PI mathematical constant pi को पूरी floating-point precision के साथ देता है, जो circle का area या circumference जैसे geometry calculations के लिए उपयोगी है, बिना इसके digits खुद याद रखे या टाइप किए।

Note: हार्डकोड किए गए 3.14 जैसे किसी approximation के बजाय Math.PI इस्तेमाल करना यह सुनिश्चित करता है कि आपकी calculations JavaScript के number type की अधिकतम precision जितनी सटीक रहें।
Warning: Math.PI एक read-only property है, function नहीं, इसे बिना parentheses के access किया जाता है, Math.PI, न कि Math.PI()।

उदाहरण: Math.PI and Other Constants

javascript
console.log(Math.PI);
const radius = 5;
console.log(Math.PI * radius * radius); // circle area
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. Math.round(), Math.floor(), और Math.ceil() को confuse करना, वे क्रमशः nearest, down, और up की ओर अलग तरीके से round करते हैं।
  2. यह भूल जाना कि Math.random() 0 और 1 के बीच एक decimal return करता है, जिसे उपयोगी range में scale करने के लिए extra math चाहिए।
  3. यह मान लेना कि Math.max() या Math.min() किसी array पर सीधे काम करते हैं, उन्हें individual arguments या spread operator चाहिए।
चैप्टर सारांश
  • Math.round, Math.floor, और Math.ceil numbers को क्रमशः nearest, lower, या upper whole number तक round करते हैं।
  • Math.abs किसी number का positive version return करता है, और Math.max/Math.min values के किसी set में सबसे बड़ी या सबसे छोटी value ढूंढते हैं।
  • Math.random एक random decimal generate करता है, और Math.pow/Math.sqrt powers और square roots handle करते हैं, constant Math.PI के साथ।
ब्राउज़र सपोर्ट

Math object और उसके सभी standard methods core JavaScript features हैं जो हर browser में समान रूप से supported हैं।

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.