JS का Math
In this page:
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 के कितना भी करीब क्यों न हो।
उदाहरण: Rounding: round, floor, and ceil
// 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 के साथ मिलाकर इस्तेमाल होते हैं।
उदाहरण: abs, max, and min
// 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 किया जाता है।
उदाहरण: Math.random()
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 के साथ काम करता है।
उदाहरण: pow and sqrt
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 खुद याद रखे या टाइप किए।
उदाहरण: Math.PI and Other Constants
console.log(Math.PI);
const radius = 5;
console.log(Math.PI * radius * radius); // circle area
- Math.round(), Math.floor(), और Math.ceil() को confuse करना, वे क्रमशः nearest, down, और up की ओर अलग तरीके से round करते हैं।
- यह भूल जाना कि Math.random() 0 और 1 के बीच एक decimal return करता है, जिसे उपयोगी range में scale करने के लिए extra math चाहिए।
- यह मान लेना कि 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 हैं।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic