JavaScript Number Properties
JavaScript में numbers से जुड़ी कुछ static properties होती हैं, जो हमें maximum/minimum values, constants जैसे Infinity और NaN, और numerical limits को समझने में मदद करती हैं।
इन properties का उपयोग Number object के साथ किया जाता है और ये read-only (immutable) होती हैं।
इस अध्याय में आप सीखेंगे:
- JavaScript के built-in Number constants
- इनका उपयोग और output
- Edge cases और comparison
- अभ्यास प्रश्न
🔹 Number Properties – Overview
Property | Description |
---|---|
Number.MAX_VALUE | सबसे बड़ा positive number जो JS handle कर सकता है |
Number.MIN_VALUE | सबसे छोटा positive (non-zero) number (negative नहीं) |
Number.POSITIVE_INFINITY | Infinity को represent करता है |
Number.NEGATIVE_INFINITY | -Infinity को represent करता है |
Number.NaN | NaN value को represent करता है (Not-a-Number ) |
Number.EPSILON | दो floating-point numbers के बीच का सबसे छोटा अंतर |
Number.MAX_SAFE_INTEGER | सबसे बड़ा safe integer (2⁵³ – 1) |
Number.MIN_SAFE_INTEGER | सबसे छोटा safe integer (-2⁵³ + 1) |
🔸 1. Number.MAX_VALUE
console.log(Number.MAX_VALUE);
// Output: 1.7976931348623157e+308
इससे बड़ा कोई number Infinity बन जाता है।
🔸 2. Number.MIN_VALUE
console.log(Number.MIN_VALUE);
// Output: 5e-324
यह 0 के सबसे पास का positive number होता है।
🔸 3. Number.POSITIVE_INFINITY
& Number.NEGATIVE_INFINITY
console.log(Number.POSITIVE_INFINITY); // Output: Infinity
console.log(Number.NEGATIVE_INFINITY); // Output: -Infinity
जब कोई number calculation limit से बाहर हो जाए, तो यह value return होती है।
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
🔸 4. Number.NaN
console.log(Number.NaN); // Output: NaN
console.log(isNaN(Number.NaN)); // Output: true
console.log(typeof Number.NaN); // Output: "number"
NaN का type भी “number” ही होता है। यह तब आता है जब कोई invalid arithmetic हो।
🔸 5. Number.EPSILON
console.log(Number.EPSILON);
// Output: 2.220446049250313e-16
यह दो floating-point numbers के बीच का सबसे छोटा अंतर है।
Useful when comparing decimal values:
let a = 0.1 + 0.2;
let b = 0.3;
console.log(Math.abs(a - b) < Number.EPSILON); // Output: true
🔸 6. Number.MAX_SAFE_INTEGER
& Number.MIN_SAFE_INTEGER
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991
इन limits के बाहर वाले integers “unsafe” माने जाते हैं क्योंकि उनकी precision lost हो सकती है।
📋 Summary Table
Property | Approx. Value | क्या दर्शाता है |
---|---|---|
Number.MAX_VALUE | 1.7976931348623157e+308 | सबसे बड़ा JS number |
Number.MIN_VALUE | 5e-324 | सबसे छोटा positive number |
Number.POSITIVE_INFINITY | Infinity | अनंत (positive) |
Number.NEGATIVE_INFINITY | -Infinity | अनंत (negative) |
Number.NaN | NaN | Not a Number |
Number.EPSILON | 2.220446049250313e-16 | Decimal तुलना में अंतर |
Number.MAX_SAFE_INTEGER | 9007199254740991 | Safe maximum integer |
Number.MIN_SAFE_INTEGER | -9007199254740991 | Safe minimum integer |
अभ्यास प्रश्न
Number.MAX_VALUE
औरNumber.MAX_SAFE_INTEGER
में क्या अंतर है?- JavaScript में Infinity कैसे उत्पन्न होती है? एक उदाहरण दीजिए।
Number.EPSILON
का उपयोग क्यों और कब किया जाता है?- नीचे दिए गए code का output क्या होगा?
console.log(0.1 + 0.2 === 0.3); console.log(Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON);
- क्या
typeof NaN
"number"
होता है? इसे समझाइए। Number.MIN_VALUE
और0
में क्या अंतर है?