| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/BigInt | [Back] [Original] |
Get to know MDN better
BigInt BigInt (bigint) n BigInt() new
const previouslyMaxSafeInteger = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
// 9007199254740991n
const hugeString = BigInt("9007199254740991");
// 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff");
// 9007199254740991n
const hugeOctal = BigInt("0o377777777777777777");
// 9007199254740991n
const hugeBin = BigInt(
"0b11111111111111111111111111111111111111111111111111111",
);
// 9007199254740991n
typeof (bigint ) "bigint"
typeof 1n === "bigint"; // true
typeof BigInt("1") === "bigint"; // true
Object
typeof Object(1n) === "object"; // true
BigInt
2
+) /) 0const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER); // 9007199254740991n
const maxPlusOne = previousMaxSafe + 1n; // 9007199254740992n
const theFuture = previousMaxSafe + 2n; // 9007199254740993n
const prod = previousMaxSafe * 2n; // 18014398509481982n
const diff = prod - 10n; // 18014398509481972n
const mod = prod % 10n; // 2n
const bigN = 2n ** 54n; // 18014398509481984n
bigN * -1n; // -18014398509481984n
const expected = 4n / 2n; // 2n
const truncated = 5n / 2n; // 2n 2.5n
0n === 0; // false
0n == 0; // true
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 ]
mixed.sort((a, b) => a - b);
//
// TypeError: can't convert BigInt value to Number value
//
mixed.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
// [ -12n, 0, 0n, 4n, 4, 6, 10 ]
Object
Object(0n) === 0n; // false
Object(0n) === Object(0n); // false
const o = Object(0n);
o === o; // true
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
JavaScript 101n ** 65537n 17n ** 9999n FAQ
JSON.stringify() TypeError JSON JSON.stringify() toJSON() toJSON()
BigInt.prototype.toJSON = function () {
return { $bigint: this.toString() };
};
JSON.stringify()
console.log(JSON.stringify({ a: 1n }));
// {"a":{"$bigint":"1"}}
BigInt.prototype JSON.stringify() replacer
const replacer = (key, value) =>
typeof value === "bigint" ? { $bigint: value.toString() } : value;
const data = {
number: 1,
big: 18014398509481982n,
};
const stringified = JSON.stringify(data, replacer);
console.log(stringified);
// {"number":1,"big":{"$bigint":"18014398509481982"}}
JSON JSON.parse reviver
const reviver = (key, value) =>
value !== null &&
typeof value === "object" &&
"$bigint" in value &&
typeof value.$bigint === "string"
? BigInt(value.$bigint)
: value;
const payload = '{"number":1,"big":{"$bigint":"18014398509481982"}}';
const parsed = JSON.parse(payload, reviver);
console.log(parsed);
// { number: 1, big: 18014398509481982n }
:
JSON.stringify() replacer JSON.parse() reviver
undefined null TypeError true 1n false 0n SyntaxError TypeError TypeError [Symbol.toPrimitive]() "number" valueOf()toString() JavaScript BigInt() BigInt(x) x TypeError
BigInt.asIntN() BigInt.asUintN() BigInt64Array BigUint64Array
BigInt()new
BigInt.prototype BigInt
BigInt.prototype.constructorBigInt BigInt
BigInt.prototype[Symbol.toStringTag][Symbol.toStringTag] "BigInt" Object.prototype.toString() BigInt toString() thisArg Object.prototype.toString.call()
function isPrime(n) {
if (n < 2n) {
return false;
}
if (n % 2n === 0n) {
return n === 2n;
}
for (let factor = 3n; factor * factor <= n; factor += 2n) {
if (n % factor === 0n) {
return false;
}
}
return true;
}
// nth
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 2027 LanguageSpecification # sec-bigint-objects |
| Web Proxy Viewer | New URL | Original Page |