JS का BigInt
In this page:
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
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
// 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
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
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
// 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);
- एक expression में BigInt और Number को मिलाना, जैसे
1n + 1, जोTypeErrorthrow करता है। - BigInt के साथ
Mathfunctions इस्तेमाल करना, जैसेMath.sqrt(4n), जोTypeErrorthrow करता है। - किसी BigInt पर
JSON.stringify()कॉल करना, जोTypeErrorthrow करता है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: