BigInt
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2020å¹´9æ.
坦驿§è³ª: 鿝ä¸å實é©ä¸çåè½
æ¤åè½å¨æäºç覽å¨å°å¨éç¼ä¸ï¼è«åèå
¼å®¹è¡¨æ ¼ä»¥å¾å°ä¸åç覽å¨ç¨çåè¼ã
BigInt æ¯ä¸åå
§å»ºçç©ä»¶ï¼æä¾äºè¡¨ç¤ºå¤§æ¼ 2^53 çæ´æ¸çåè½ (2^53 æ¯ JavaScript åççNumberè½å¤ 表示çæå¤§å¼)
èªæ³
BigInt(value);
忏
value-
欲åµå»ºçæ¸å¼ï¼å¯ä»¥çºæ´æ¸æå串ã
å註ï¼BigInt() ä¸å new ä¸èµ·ä½¿ç¨ã
說æ
BigInt æ¯ééå¨ä¸åæ¸å¼å¾å ä¸ n ï¼ä¾å¦ 10n ï¼æå¼å« BigInt() æçæçã
const theBiggestInt = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
// ⪠9007199254740991n
const hugeString = BigInt("9007199254740991");
// ⪠9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff");
// ⪠9007199254740991n
const hugeBin = BigInt(
"0b11111111111111111111111111111111111111111111111111111",
);
// ⪠9007199254740991n
BigInt è· Number å¾åï¼ä½å¨æäºé¨åæäºè¨±ä¸å â å®ä¸å¯ä»¥è¢«ç¨å¨å
§å»ºç Math ç©ä»¶æ¹æ³ä¸ãèä¸ä¸å¯ä»¥è· Number ç坦髿··ç¨éç®åã
è¦åï¼Number å BigInt ä¸è½æ··åè¨ç® â ä»åå¿
é è¢«è½æå°åä¸ååæ
ã
ç¶èï¼å¨ç¸äºè½ææè¦æ³¨æï¼ BigInt å¨è¢«è½ææ Number æå¯è½æéºå¤±é¨å精度çè³è¨ã
é¡å¥è³è¨
ç¶ä½¿ç¨ typeof 測試æï¼ä¸å BigInt æåå³ "bigint"ï¼
typeof 1n === "bigint"; // true
typeof BigInt("1") === "bigint"; // true
ç¶ä½¿ç¨ Object ä¾å
裹æï¼BigInt æè¢«çææ¯æ®éç "object" åæ
ï¼
typeof Object(1n) === "object"; // true
Operator
ä¸åçéç®åå¯ä»¥è¢«ç¨å¨ BigInt ä¸ (æç± object å
裹ç BigInt): +, *, -, **, %.
const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER);
// ⪠9007199254740991
const maxPlusOne = previousMaxSafe + 1n;
// ⪠9007199254740992n
const theFuture = previousMaxSafe + 2n;
// ⪠9007199254740993n, this works now!
const multi = previousMaxSafe * 2n;
// ⪠18014398509481982n
const subtr = multi â 10n;
// ⪠18014398509481972n
const mod = multi % 10n;
// ⪠2n
const bigN = 2n ** 54n;
// ⪠18014398509481984n
bigN * -1n
// ⪠â18014398509481984n
/ éç®åä¹å樣çè½å¤ éè¡ãç¶èï¼å çºåæ
æ¯ BigInt è䏿¯ BigDecimal ï¼é¤æ³éç®æç¡æ¢ä»¶æ¨å»å°æ¸ãä¹å°±æ¯èªªï¼åå³å¼ä¸æå
å«å°æ¸é¨åã
è¦åï¼åå³å¼å¸¶å°æ¸çéç®å¨ä½¿ç¨BigInt æå°æ¸é¨åæè¢«æ¨å»ã
const expected = 4n / 2n;
// ⪠2n
const rounded = 5n / 2n;
// ⪠2n, not 2.5n
æ¯è¼
ä¸å BigInt 並ä¸å´æ ¼çæ¼ä¸å Numberï¼ä½ä»åæä¸è¬ç¸çã
0n === 0;
// ⪠false
0n == 0;
// ⪠true
ä¸å Number å BigInt å¯ä»¥åæ®ééç®ä¸æ¨£æ¯è¼ã
1n < 2;
// ⪠true
2n > 1;
// ⪠true
2 > 2;
// ⪠false
2n > 2;
// ⪠false
2n >= 2;
// ⪠true
ä»åå¯ä»¥åéå¨é£åä¸ä¸¦ç §é æç被æåºã
const mixed = [4n, 6, -12n, 10, 4, 0, 0n];
// ⪠[4n, 6, -12n, 10, 4, 0, 0n]
mixed.sort();
// ⪠[-12n, 0, 0n, 10, 4n, 4, 6]
Note that comparisons with Object-wrapped BigInts act as with other objects, only indicating equality when the same object instance is compared:
0n === Object(0n); // false
Object(0n) === Object(0n); // false
const o = Object(0n);
o === o; // true
Conditional
A BigInt behaves like a Number in cases where it is converted to a Boolean: via the Boolean function; when used with logical operators Logical Operators ||, &&, and !; or within a conditional test like an if statement.
if (0n) {
console.log("Hello from the if!");
} else {
console.log("Hello from the else!");
}
// ⪠"Hello from the else!"
0n || 12n;
// ⪠12n
0n && 12n;
// ⪠0n
Boolean(0n);
// ⪠false
Boolean(12n);
// ⪠true
!12n;
// ⪠false
!0n;
// ⪠true
æ¹æ³
BigInt.asIntN()-
Clamps a BigInt value to a signed integer value, and returns that value.
BigInt.asUintN()-
Clamps a BigInt value to an unsigned integer value, and returns that value.
屬æ§
BigInt.prototype-
å 許å°ä¸å
BigIntç©ä»¶å¢å å ¶å±¬æ§ã
BigInt ç©ä»¶å¯¦é«
All BigInt instances inherit from BigInt.prototype. The prototype object of the BigInt constructor can be modified to affect all BigInt instances.
æ¹æ³
BigInt.prototype.toLocaleString()-
Returns a string with a language-sensitive representation of this BigInt value. Overrides the
Object.prototype.toLocaleString()method. BigInt.prototype.toString()-
Returns a string representing this BigInt value in the specified radix (base). Overrides the
Object.prototype.toString()method. BigInt.prototype.valueOf()-
Returns this BigInt value. Overrides the
Object.prototype.valueOf()method.
建è°ç¨æ³
>è½å
å çºå¨ Number å BigInt ä¹éè½æå¯è½é æç²¾åº¦éºå¤±ï¼å»ºè°ç¶æ¸å¼æè¶
é 2^53 æåªä½¿ç¨ BigInt ï¼èä¸è¦å¨å
©è
ä¹éé²è¡è½æã
å å¯
BigInt æ¯æ´çéç®ä¸¦éå¸¸æ¸æéãå æ¤ BigInt ä¸é©ç¨å¨å å¯å¸ä¸ã
ç¯ä¾
>è¨ç®è³ªæ¸
function isPrime(p) {
for (let i = 2n; i * i <= p; i++) {
if (p % i === 0n) return false;
}
return true;
}
// Takes a BigInt as an argument and returns a BigInt
function nthPrime(nth) {
let maybePrime = 2n;
let prime = 0n;
while (nth >= 0n) {
if (isPrime(maybePrime)) {
nth -= 1n;
prime = maybePrime;
}
maybePrime += 1n;
}
return prime;
}
nthPrime(20n);
// ⪠73n
è¦ç¯
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-bigint-objects> |