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

BigInt - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

BigInt

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2020 9.

BigInt Number 2^53 - 1 .

In this article

BigInt n (10n) BigInt() .

js
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 . BigInt Math , Number . . , BigInt Number .

BigInt typeof "bigint".

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

Object BigInt object .

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

+, *, -, **, % BigInt BigInt . , BigInt >>> ( ) . asm.js , + .

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 , . , .

: BigInt .

js
const expected = 4n / 2n;
//  2n

const rounded = 5n / 2n;
//  2.5n  2n

BigInt Number .

js
0n === 0;
//  false

0n == 0;
//  true

Number BigInt .

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]

Object BigInt .

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

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

BigInt Number .

js
if (0n) {
  console.log("if !");
} else {
  console.log("else !");
}

//  "else !"

0n || 12n;
//  12n

0n && 12n;
//  0n

Boolean(0n);
//  false

Boolean(12n);
//  true

!12n;
//  false

!0n;
//  true

BigInt()

BigInt .

BigInt.asIntN()

BigInt -2^(width - 1) 2^(width - 1) - 1 .

BigInt.asUintN()

BigInt 0 2^width - 1 .

BigInt.prototype.toLocaleString()

BigInt . Object.prototype.toLocaleString() .

BigInt.prototype.toString()

BigInt . Object.prototype.toString() .

BigInt.prototype.valueOf()

BigInt . Object.prototype.valueOf() .

BigInt Number , 2^53 BigInt .

BigInt .

JSON

BigInt , JSON.stringify() BigInt TypeError . , toJSON .

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

.

js
JSON.stringify(BigInt(1));
// '"1"'

js
//  BigInt  true 
function isPrime(p) {
  for (let i = 2n; i * i <= p; i++) {
    if (p % i === 0n) return false;
  }
  return true;
}

// BigInt , n  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

Specification
ECMAScript 2027 LanguageSpecification
# sec-bigint-objects


Web Proxy Viewer  |  New URL  |  Original Page