इस ट्यूटोरियल में, आप उदाहरणों की सहायता से जावास्क्रिप्ट संख्या के बारे में जानेंगे।
जावास्क्रिप्ट में, संख्याएँ आदिम डेटा प्रकार हैं। उदाहरण के लिए,
const a = 3; const b = 3.13;
कुछ अन्य प्रोग्रामिंग भाषाओं के विपरीत, आपको विशेष रूप से इंट , फ्लोट आदि का उपयोग करके पूर्णांक या फ्लोटिंग मानों की घोषणा करने की आवश्यकता नहीं है ।
आप बड़ी या बहुत छोटी संख्याओं को शामिल करने के लिए घातीय संकेतन ई का उपयोग कर सकते हैं । उदाहरण के लिए,
const a1 = 5e9; console.log(a1); //5000000000 const a2 = 5e-5; console.log(a2); // 0.00005
हेक्साडेसिमल संकेतन में संख्याओं को भी निरूपित किया जा सकता है। उदाहरण के लिए,
const a = 0xff; console.log(a); // 255 const b = 0x00 ; console.log(b); // 0
+ संचालक संख्या के साथ
जब +
संख्याओं के साथ उपयोग किया जाता है, तो इसका उपयोग संख्याओं को जोड़ने के लिए किया जाता है। उदाहरण के लिए,
const a = 4 + 9; console.log(a); // 13
जब +
संख्याओं और तारों के साथ प्रयोग किया जाता है, तो उन्हें समेटने के लिए उपयोग किया जाता है। उदाहरण के लिए,
const a = '4' + 9; console.log(a); // 49
जब एक संख्यात्मक स्ट्रिंग का उपयोग अन्य संख्यात्मक कार्यों के साथ किया जाता है, तो संख्यात्मक स्ट्रिंग को एक संख्या में बदल दिया जाता है। उदाहरण के लिए,
const a = '4' - 2; console.log(a); // 2 const a = '4' / 2; console.log(a); // 2 const a = '4' * 2; console.log(a); // 8
जावास्क्रिप्ट NaN
जावास्क्रिप्ट में, NaN
(संख्या नहीं) एक कीवर्ड है जो इंगित करता है कि मान एक संख्या नहीं है।
+
में स्ट्रिंग परिणाम के साथ अंकगणितीय मान को छोड़कर अंकगणितीय संचालन (छोड़कर ) करना NaN
। उदाहरण के लिए,
const a = 4 - 'hello'; console.log(a); // NaN
isNaN()
यदि कोई मान एक संख्या है, तो अंतर्निहित फ़ंक्शन का उपयोग किया जा सकता है। उदाहरण के लिए,
const a = isNaN(9); console.log(a); // false const a = isNaN(4 - 'hello'); console.log(a); // true
जब typeof
ऑपरेटर का उपयोग NaN
मूल्य के लिए किया जाता है , तो यह एक नंबर आउटपुट देता है। उदाहरण के लिए,
const a = 4 - 'hello'; console.log(a); // NaN console.log(typeof a); // "number"
जावास्क्रिप्ट इन्फिनिटी
जावास्क्रिप्ट में, जब गणना की जाती है जो सबसे बड़ी (या सबसे छोटी) संभव संख्या से अधिक है, Infinity
(या -Infinity
) वापस आ जाती है। उदाहरण के लिए,
const a = 2 / 0; console.log(a); // Infinity const a = -2 / 0; console.log(a); // -Infinity
जावास्क्रिप्ट बिगआईंट
जावास्क्रिप्ट में संख्या प्रकार केवल कम से कम संख्या का प्रतिनिधित्व कर सकते (2 53 - 1) की तुलना में अधिक है और - (2 53 - 1) । हालाँकि, यदि आपको इससे बड़ी संख्या का उपयोग करने की आवश्यकता है, तो आप BigInt डेटा प्रकार का उपयोग कर सकते हैं।
एक पूर्णांक के अंत में n जोड़कर एक BigInt संख्या बनाई जाती है । उदाहरण के लिए,
// BigInt value const value = 900719925124740998n; // Adding two big integers const value1 = value + 1n; console.log(value1); // returns "900719925124740999n"
नोट: BigInt को जावास्क्रिप्ट के नए संस्करण में पेश किया गया था और यह कई ब्राउज़रों द्वारा समर्थित नहीं है। अधिक जानने के लिए जावास्क्रिप्ट बिगआईंट समर्थन पर जाएँ।
जावास्क्रिप्ट संख्याएँ 64-बिट में संग्रहित हैं
जावास्क्रिप्ट में, संख्याओं को 64-बिट प्रारूप IEEE-754 में संग्रहीत किया जाता है, जिसे "डबल सटीक फ्लोटिंग पॉइंट नंबर" के रूप में भी जाना जाता है।
संख्या 64 बिट्स में संग्रहीत की जाती है (संख्या 0 से 51 बिट स्थिति में संग्रहीत होती है, 52 से 62 बिट पदों में घातांक और 63 बिट स्थिति में संकेत)।
संख्या | प्रतिपादक | संकेत |
---|---|---|
52 बिट्स (0 - 51) | 11 बिट्स (52- 62) | 1 बिट (63) |
सटीक समस्याएँ
फ्लोटिंग-पॉइंट नंबरों पर संचालन से कुछ अप्रत्याशित परिणाम प्राप्त होते हैं। उदाहरण के लिए,
const a = 0.1 + 0.2; console.log(a); // 0.30000000000000004
परिणाम 0.30000000000000004 के बजाय 0.3 होना चाहिए । यह त्रुटि इसलिए होती है क्योंकि जावास्क्रिप्ट में, अंकों को द्विआधारी रूप में संग्रहीत किया जाता है ताकि दशमलव अंकों को आंतरिक रूप से दर्शाया जा सके। और दशमलव संख्याओं को द्विआधारी रूप में बिल्कुल प्रतिनिधित्व नहीं किया जा सकता है।
उपरोक्त समस्या को हल करने के लिए, आप ऐसा कुछ कर सकते हैं:
const a = (0.1 * 10 + 0.2 * 10) / 10; console.log(a); // 0.3
आप toFixed()
विधि का उपयोग भी कर सकते हैं ।
const a = 0.1 + 0.2; console.log(a.toFixed(2)); // 0.30
toFixed(2)
rounds up the decimal number to two decimal values.
const a = 9999999999999999 console.log(a); // 10000000000000000
Note: Integers are accurate up to 15 digits.
Number Objects
You can also create numbers using the new
keyword. For example,
const a = 45; // creating a number object const b = new Number(45); console.log(a); // 45 console.log(b); // 45 console.log(typeof a); // "number" console.log(typeof b); // "object"
Note: It is recommended to avoid using number objects. Using number objects slows down the program.
JavaScript Number Methods
Here is a list of built-in number methods in JavaScript.
Method | Description |
---|---|
isNaN() | determines whether the passed value is NaN |
isFinite() | determines whether the passed value is a finite number |
isInteger() | determines whether the passed value is an integer |
isSafeInteger() | determines whether the passed value is a safe integer |
parseFloat(string) | converts the numeric floating string to floating-point number |
parseInt(string, (radix)) | converts the numeric string to integer |
toExponential(fractionDigits) | returns a string value for a number in exponential notation |
toFixed(digits) | returns a string value for a number in fixed-point notation |
toPrecision() | returns a string value for a number to a specified precision |
toString((radix)) | returns a string value in a specified radix(base) |
valueof() | returns the numbers value |
toLocaleString() | returns a string with a language sensitive representation of a number |
For example,
// check if a is integer const a = 12; console.log(Number.isInteger(a)); // true // check if b is NaN const b = NaN; console.log(Number.isNaN(b)); // true // display upto two decimal point const d = 5.1234; console.log(d.toFixed(2)); // 5.12
JavaScript Number Properties
Here is a list of Number properties in JavaScript.
Property | Description |
---|---|
EPSILON | returns the smallest interval between two representable numbers |
MAX_SAFE_INTEGER | returns the maximum safe integer |
MAX_VALUE | returns the largest possible value |
MIN_SAFE_INTEGER | returns the minimum safe integer |
MIN_VALUE | returns the smallest possible value |
NaN | represents 'Not-a-Number' value |
NEGATIVE_INFINITY | represents negative infinity |
POSITIVE_INFINITY | सकारात्मक अनंत का प्रतिनिधित्व करता है |
प्रोटोटाइप | संख्या वस्तुओं के गुणों को जोड़ने की अनुमति देता है |
उदाहरण के लिए,
// largest possible value const a = Number.MAX_VALUE; console.log(a); // 1.7976931348623157e+308 // maximum safe integer const a = Number.MAX_SAFE_INTEGER; console.log(a); // 9007199254740991
जावास्क्रिप्ट संख्या () फ़ंक्शन
Number()
समारोह संख्या के लिए विभिन्न डेटा प्रकार परिवर्तित करने के लिए प्रयोग किया जाता है। उदाहरण के लिए,
const a = '23'; // string const b = true; // boolean //converting to number const result1 = Number(a); const result2 = Number(b); console.log(result1); // 23 console.log(result2); // 1
यदि आप संख्या रूपांतरण के बारे में अधिक जानना चाहते हैं, तो जावास्क्रिप्ट प्रकार रूपांतरण पर जाएँ।