BigInt
is a built-in object that provides a way to represent whole numbers larger than 2
53
- 1, which is the largest number JavaScript can reliably represent with the
Number
primitive
and represented by the
Number.MAX_SAFE_INTEGER
constant.
BigInt
can be used for arbitrarily large integers.
A
BigInt
is created by appending
n
to the end of an integer literal —
10n
— or by calling the function
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
in some ways, but also differs in a few key matters — it cannot be used with methods in the built-in
Math
object and cannot be mixed with instances of
Number
in operations; they must be coerced to the same type. Be careful coercing values back and forth, however, as the precision of a
BigInt
may be lost when it is coerced to a
Number
.
When tested against
typeof
,
BigInt
will give "bigint":
typeof 1n === 'bigint' // true
typeof BigInt('1') === 'bigint' // true
When wrapped in an
Object
,
BigInt
will be considered as a normal "object" type:
typeof Object(1n) === 'object' // true
The following operators may be used with
BigInt
s (or object-wrapped
BigInt
s):
+
,
*
,
-
,
**
,
%
.
Bitwise operators
are supported as well, except
>>>
(zero-fill right shift) as all BigInts are signed.
Also unsupported is the unary operator (
+
),
in order to not break asm.js
.
const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER) // ↪ 9007199254740991n 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
/
operator also works as expected with whole numbers.
However, since these are
BigInt
s and not
BigDecimal
s, this operation will round towards
0
(which is to say, it will not return any fractional digits).
An operation with a fractional result will be truncated when used with a
BigInt
.
const expected = 4n / 2n // ↪ 2n const rounded = 5n / 2n // ↪ 2n, not 2.5n
A
BigInt
is not strictly equal to a
Number
, but it
is
loosely so:
0n === 0 // ↪ false 0n == 0 // ↪ true
A
Number
和
BigInt
may be compared as usual:
1n < 2 // ↪ true 2n > 1 // ↪ true 2 > 2 // ↪ false 2n > 2 // ↪ false 2n >= 2 // ↪ true
They may be mixed in arrays and sorted:
const mixed = [4n, 6, -12n, 10, 4, 0, 0n] // ↪ [4n, 6, -12n, 10, 4, 0, 0n] mixed.sort() // default sorting behavior // ↪ [ -12n, 0, 0n, 10, 4n, 4, 6 ] mixed.sort((a, b) => a - b) // won't work since subtraction will not work with mixed types // TypeError: can't convert BigInt to number // sort with an appropriate numeric comparator mixed.sort((a, b) => (a < b) ? -1 : ((a > b) ? 1 : 0)) // ↪ [ -12n, 0, 0n, 4n, 4, 6, 10 ]
Note that comparisons with
Object
-wrapped
BigInt
s 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
A
BigInt
behaves like a
Number
in cases where:
布尔
: via the
布尔
function;
||
,
&&
,和
!
; or
if
语句。
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()
bigint
值。
BigInt.asIntN()
BigInt
value to a signed integer between
-2
width
-1
and
2
width
-1
- 1
.
BigInt.asUintN()
BigInt
value to an unsigned integer between
0
and
2
width
- 1
.
BigInt.prototype.toLocaleString()
Object.prototype.toLocaleString()
方法。
BigInt.prototype.toString()
Object.prototype.toString()
方法。
BigInt.prototype.valueOf()
Object.prototype.valueOf()
方法。
Because coercing between
Number
and
BigInt
can lead to loss of precision, it is recommended to only use
BigInt
when values greater than 2
53
are reasonably expected and not to coerce between the two types.
The operations supported on
BigInt
s are not constant time.
BigInt
is therefore
unsuitable for use in cryptography
.
使用
JSON.stringify()
with any
BigInt
value will raise a
TypeError
as
BigInt
values aren't serialized in JSON by default. However, you can implement your own
toJSON
method if needed:
BigInt.prototype.toJSON = function() { return this.toString() }
Instead of throwing,
JSON.stringify
now produces a string like this:
JSON.stringify(BigInt(1)) // '"1"'
// Returns true if passed BigInt is a prime number
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, returns nth prime number as BigInt
function nthPrime(nth) {
let maybePrime = 2n
let prime = 0n
while (nth >= 0n) {
if (isPrime(maybePrime)) {
nth--
prime = maybePrime
}
maybePrime++
}
return prime
}
nthPrime(20n)
// ↪ 73n
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of '
BigInt
objects' in that specification.
|
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
BigInt
|
Chrome 67 | Edge 79 | Firefox 68 | IE No | Opera 54 | Safari 14 | WebView Android 67 | Chrome Android 67 | Firefox Android 68 | Opera Android 48 | Safari iOS 14 | Samsung Internet Android 9.0 | nodejs 10.4.0 |
BigInt()
构造函数
|
Chrome 67 | Edge 79 | Firefox 68 | IE No | Opera 54 | Safari 14 | WebView Android 67 | Chrome Android 67 | Firefox Android 68 | Opera Android 48 | Safari iOS 14 | Samsung Internet Android 9.0 | nodejs 10.4.0 |
asIntN
|
Chrome 67 | Edge 79 | Firefox 68 | IE No | Opera 54 | Safari 14 | WebView Android 67 | Chrome Android 67 | Firefox Android 68 | Opera Android 48 | Safari iOS 14 | Samsung Internet Android 9.0 | nodejs 10.4.0 |
asUintN
|
Chrome 67 | Edge 79 | Firefox 68 | IE No | Opera 54 | Safari 14 | WebView Android 67 | Chrome Android 67 | Firefox Android 68 | Opera Android 48 | Safari iOS 14 | Samsung Internet Android 9.0 | nodejs 10.4.0 |
toLocaleString
|
Chrome 67 | Edge 79 | Firefox 68 | IE No | Opera 54 | Safari 14 | WebView Android 67 | Chrome Android 67 | Firefox Android 68 | Opera Android 48 | Safari iOS 14 | Samsung Internet Android 9.0 | nodejs 10.4.0 |
toString
|
Chrome 67 | Edge 79 | Firefox 68 | IE No | Opera 54 | Safari 14 | WebView Android 67 | Chrome Android 67 | Firefox Android 68 | Opera Android 48 | Safari iOS 14 | Samsung Internet Android 9.0 | nodejs 10.4.0 |
valueOf
|
Chrome 67 | Edge 79 | Firefox 68 | IE No | Opera 54 | Safari 14 | WebView Android 67 | Chrome Android 67 | Firefox Android 68 | Opera Android 48 | Safari iOS 14 | Samsung Internet Android 9.0 | nodejs 10.4.0 |
完整支持
不支持
The following table provides a daily implementation status for this feature, because this feature has not yet reached cross-browser stability. The data is generated by running the relevant feature tests in Test262 , the standard test suite of JavaScript, in the nightly build, or latest release of each browser's JavaScript engine.
BigInt
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()