← Back to JavaScript Course | Chapter 5: ES6+ Features | Lesson 11 of 12

JS का BigInt

साधारण JavaScript numbers एक calculator display जैसे हैं जिनमें digits की एक तय संख्या होती है; जब कोई whole number काफी बड़ा हो जाता है, तो वह precision खोने लगता है क्योंकि जगह खत्म हो जाती है। BigInt एक दूसरा numeric type है जो ज़रूरत के हिसाब से अपनी digit tape बढ़ाता है, जो आपको लगभग असीमित size के integers represent करने देता है, जो साधारण numbers सटीकता से नहीं कर सकते।
Syntax
javascript
const bigNumber = 123n;
const bigNumber = BigInt(value);
bigA + bigB;

BigInt क्या है

BigInt साधारण Number type की precision सीमाओं से परे, arbitrary size के integers represent करने के लिए एक built-in numeric type है।

आप किसी integer के अंत में letter n जोड़कर एक BigInt literal लिखते हैं, जैसे 123n, जो JavaScript को इसे किसी floating-point Number के बजाय एक exact, असीमित-size वाला whole number मानने के लिए कहता है।

उदाहरण: What Is BigInt

javascript
const big = 123n;
console.log(typeof big, big);

BigInt Values बनाना

आप एक BigInt या तो n-suffixed literal syntax से बना सकते हैं या किसी number या numeric string पर BigInt() function कॉल करके।

function form तब उपयोगी है जब आपको runtime पर पहले से एक साधारण Number या string के रूप में store की गई value को BigInt में बदलना हो।

उदाहरण: Creating BigInt Values

javascript
// Declare the constant `a`, set to `123n`
// Declare the constant `a`, set to `123n`
const a = 123n;
// Declare the constant `b`, set to `BigInt(456)`
// Declare the constant `b`, set to `BigInt(456)`
const b = BigInt(456);
// Declare the constant `c`, set to `BigInt("789")`
// Declare the constant `c`, set to `BigInt("789")`
const c = BigInt("789");
// Print `a, b, c` to the console
// Print `a, b, c` to the console
console.log(a, b, c);

BigInt Arithmetic

BigInt standard arithmetic operators +, -, *, और / को support करता है, पर division हमेशा zero की ओर truncate करता है और किसी भी remainder को छोड़ देता है, क्योंकि BigInt में fractional part जैसी कोई चीज़ नहीं है।

इसका मतलब है कि 7n / 2n का evaluation 3n होता है, 3.5 नहीं, जो decimal division की उम्मीद करने वाले किसी को भी हैरान कर सकता है।

उदाहरण: BigInt Arithmetic

javascript
console.log(7n / 2n); // 3n, truncated toward zero, not 3.5

BigInt और Number को मिलाना

JavaScript जानबूझकर एक TypeError देता है अगर आप किसी BigInt और साधारण Number के बीच सीधे arithmetic operators इस्तेमाल करने की कोशिश करें, क्योंकि दोनों के बीच implicitly convert करना चुपचाप किसी भी तरफ precision खो सकता है।

उन्हें मिलाने से पहले आपको एक तरफ को BigInt() या Number() से explicitly convert करना होगा।

उदाहरण: Mixing BigInt and Number

javascript
try {
  console.log(5n + 5); // TypeError
} catch (e) {
  console.log(e.message);
}
console.log(5n + BigInt(5)); // works after explicit conversion

BigInt कब इस्तेमाल करें

BigInt सही tool है जब किसी program को लगभग 9 quadrillion से आगे exact integer arithmetic चाहिए, जैसे cryptographic key calculations, high-precision timestamps, या 64-bit integers इस्तेमाल करने वाले systems से import की गई unique identifiers।

रोज़मर्रा की counting और math के लिए, साधारण Numbers अभी भी सरल और तेज़ हैं।

उदाहरण: When to Use BigInt

javascript
// Use BigInt for exact integers beyond ~9 quadrillion:
const id = 9007199254740993n;
console.log(id);
// Regular numbers are simpler for everyday counting:
const count = 5;
console.log(count);
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. एक expression में BigInt और Number को मिलाना, जैसे 1n + 1, जो TypeError throw करता है।
  2. BigInt के साथ Math functions इस्तेमाल करना, जैसे Math.sqrt(4n), जो TypeError throw करता है।
  3. किसी BigInt पर JSON.stringify() कॉल करना, जो TypeError throw करता है।

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.