--- title: BigInt slug: Web/JavaScript/Reference/Global_Objects/BigInt tags: - BigInt - JavaScript translation_of: Web/JavaScript/Reference/Global_Objects/BigInt ---
BigInt
は組み込みオブジェクトで、 {{JSxRef("Number")}} プリミティブで表現できる最大の数、 {{JSxRef("Number.MAX_SAFE_INTEGER")}} よりも大きな数値を信頼できるものとして表現する方法を提供します。 BigInt
は任意に巨大な整数に使用することができます。
BigInt
は10進数の整数リテラルの末尾に 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
はいくつかの点で {{JSxRef("Number")}} と似ていますが、重要ないくつかの点が異なります。 — 組込みの {{JSxRef("Math")}} オブジェクト内のメソッドでは利用できず、演算で Number
のインスタンスと混ぜることができません。同じ型に統一する必要があります。ただし、BigInt
を Number
へ変換する際には精度が落ちることがあるので、相互に変化する場合には注意が必要です。
typeof
の BigInt
に対する評価値は、"bigint" となります。
typeof 1n === 'bigint'; // true typeof BigInt('1') === 'bigint'; // true
Object
でラップされている場合は、 BigInt
は通常の "object" として扱われます。
typeof Object(1n) === 'object'; // true
BigInt
(または Object でラップした BigInt
) を利用することができる演算子は、 +
, *
, -
, **
, %
です。ビット操作演算子は、同様に利用できますが、 >>>
(論理的右シフト) は BigInt が常に符号付きなので除きます。同様に、単項演算子 (+
) は asm.js を破らないように対応していません。
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
ではないため、演算結果は 0 の方向に丸められます。別の言い方をすれば、小数を返すことはありません。
BigInt
を使用した場合、結果が小数となるような演算は切り捨てられます。
const expected = 4n / 2n; // ↪ 2n const rounded = 5n / 2n; // ↪ 2n, not 2.5n
BigInt
は {{JSxRef("Number")}} と厳密等価ではありませんが、等価にはなります。
0n === 0 // ↪ false 0n == 0 // ↪ true
{{JSxRef("Global_Objects/Number", "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, 4n, 4, 6, 10] 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 ]
なお、 Object
にラップされた BigInt
は他のオブジェクトと同様の振る舞いをします。同じインスタンス同士が比較された場合にのみ、等価となります:
0n === Object(0n); // false Object(0n) === Object(0n); // false const o = Object(0n); o === o // true
BigInt
が {{JSxRef("Global_Objects/Boolean", "Boolean")}} へ変換される次のような場合は、 {{JSxRef("Global_Objects/Number", "Number")}} と同様の変換が行われます。
||
、`&&
`、!
で使用される場合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
{{JSxRef("BigInt()")}}
新しいbigint
値を作ります。
BigInt
オブジェクトへの属性追加が可能です。BigInt
インスタンス全ての BigInt
インスタンスは BigInt.prototype
を継承します。このプロトタイプオブジェクトへの変更は、全ての BigInt
インスタンスに影響します。
{{page("/ja/docs/Web/JavaScript/Reference/Global_Objects/BigInt/prototype", "Methods")}}
{{JSxRef("Number")}} と BigInt
との間の型変換は精度が落ちる可能性があるため、 BigInt
は値が論理的に253以上になる場合にのみ使用し、この2つの型の間で型変換を行わないこと推奨します。
BigInt
で対応している演算は、実行時間が一定ではありません。従って、 BigInt
は暗号処理での使用には向きません。
BigInt
の値は既定で JSON のシリアライズに対応していないため、{{JSxRef("JSON.stringify()")}} を BigInt
値に対して使用すると TypeError
が発生します。ただし、必要であれば独自の toJSON
メソッドを実装することができます。
BigInt.prototype.toJSON = function() { return this.toString(); }
JSON.stringify
により、例外が発生する代わりに次のように文字列を生成するようになります。
JSON.stringify(BigInt(1)); // '"1"'
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
仕様書 |
---|
{{SpecName("ESDraft", "#sec-bigint-objects", "BigInt objects")}} |
{{Compat("javascript.builtins.BigInt")}}
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.
{{EmbedTest262ReportResultsTable("BigInt")}}