Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
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() 不和 new 一起使用。
BigInt 是透過在一個數值後加上 n ,例如 10n ,或呼叫 BigInt() 所生成的。
BigInt 跟 Number 很像,但在某些部分有些許不同 — 它不可以被用在內建的 Math 物件方法中、而且不可以跟 Number 的實體混用運算子。
警告:Number 和 BigInt 不能混和計算 — 他們必須被轉換到同一個型態。
然而,在相互轉換時要注意, BigInt 在被轉換成 Number 時可能會遺失部分精度的資訊。
當使用 typeof 測試時,一個 BigInt 會回傳 "bigint":
當使用 Object 來包裹時,BigInt 會被看成是普通的 "object" 型態:
下列的運算子可以被用在 BigInt 上 (或由 object 包裹的 BigInt): +, *, -, **, %.
/ 運算子也同樣的能夠運行。然而,因為型態是 BigInt 而不是 BigDecimal ,除法運算會無條件捨去小數。也就是說,回傳值不會包含小數部分。
警告:回傳值帶小數的運算在使用BigInt 時小數部分會被捨去。
一個 BigInt 並不嚴格等於一個 Number,但他們會一般相等。
一個 Number 和 BigInt 可以像普通運算一樣比較。
他們可以參雜在陣列中並照預期的被排序。
Note that comparisons with Object-wrapped BigInts act as with other objects, only indicating equality when the same object instance is compared:
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.
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 物件增加其屬性。
All BigInt instances inherit from BigInt.prototype. The prototype object of the BigInt constructor can be modified to affect all BigInt instances.
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 不適用在加密學上。
| ECMAScript® 2027 Language Specification # sec-bigint-objects |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年7月14日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.