[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/BigInt [Back]  [Original]

BigInt - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

BigInt

Baseline

20209

BigInt (number)

BigInt BigInt (bigint) n BigInt() new

js
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

Math

typeof (bigint ) "bigint"

js
typeof 1n === "bigint"; // true
typeof BigInt("1") === "bigint"; // true

Object

js
typeof Object(1n) === "object"; // true

BigInt

  • : +, -, *, /, %, **
  • : >>, <<, &, |, ^, ~
  • (-)
  • /: ++, --

  • : >, <, >=, <=, ==, !=, ===, !==

2

  • (+)
  • (/) 0
js
const 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 

js
0n === 0; // false
0n == 0; // true

js
1n < 2; // true
2n > 1; // true
2 > 2; // false
2n > 2; // false
2n >= 2; // true

js
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

js
Object(0n) === 0n; // false
Object(0n) === Object(0n); // false

const o = Object(0n);
o === o; // true

  • 253

0n

js
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

JSON.stringify() TypeError JSON JSON.stringify() toJSON() toJSON()

js
BigInt.prototype.toJSON = function () {
  return { $bigint: this.toString() };
};

JSON.stringify()

js
console.log(JSON.stringify({ a: 1n }));
// {"a":{"$bigint":"1"}}

BigInt.prototype JSON.stringify() replacer

js
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

js
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

JSON JavaScript 64 JSON JSON

JavaScript BigInt() BigInt(x) x TypeError

BigInt.asIntN() BigInt.asUintN() BigInt64Array BigUint64Array

BigInt()

new

BigInt.asIntN()

BigInt.asUintN()

BigInt.prototype BigInt

BigInt.prototype.constructor

BigInt BigInt

BigInt.prototype[Symbol.toStringTag]

[Symbol.toStringTag] "BigInt" Object.prototype.toString() BigInt toString() thisArg Object.prototype.toString.call()

BigInt.prototype.toLocaleString()

Object.prototype.toLocaleString()

BigInt.prototype.toString()

Object.prototype.toString()

BigInt.prototype.valueOf()

Object.prototype.valueOf()

js
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

: isPrime()

ECMAScript 2027 LanguageSpecification
# sec-bigint-objects


Web Proxy Viewer  |  New URL  |  Original Page