[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/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 ..

BigInt , 253 - 1, , JavaScript Number . , Number.MAX_SAFE_INTEGER.

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

Number, Math Number.

: Number BigInt .

, BigInt (Number).

typeof, BigInt "bigint":

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

, BigInt :

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

BigInt ( - BigInt): +, *, -, **, %.

, >>> ( ).

(+) asm.js.

js
const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER);
//  9007199254740991n

const maxPlusOne = previousMaxSafe + 1n;
//  9007199254740992n

const theFuture = previousMaxSafe + 2n;
//  9007199254740993n,  !

const multi = previousMaxSafe * 2n;
//  18014398509481982n

const subtr = multi  10n;
//  18014398509481972n

const mod = multi % 10n;
//  2n

const bigN = 2n ** 54n;
//  18014398509481984n

bigN * -1n
//  18014398509481984n

/ , , . , BigInt, , - .

: BigInt.

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

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

BigInt Number .

js
0n === 0;
//  false

0n == 0;
//  true

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]

, - BigInt , :

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

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

BigInt :

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 -2width-1 2width-1-1

BigInt.asUintN()

a BigInt 0 2width-1

BigInt.prototype.toLocaleString()

. Object.prototype.toLocaleString().

BigInt.prototype.toString()

, . Object.prototype.toString().

BigInt.prototype.valueOf()

. Object.prototype.valueOf().

Number BigInt , BigInt , , 253 .

BigInt . BigInt .

JSON

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

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

, JSON.stringify :

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

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

//  BigInt      BigInt
function nthPrime(nth) {
  let maybePrime = 2n;
  let prime = 0n;

  while (nth >= 0n) {
    if (isPrime(maybePrime)) {
      nth--;
      prime = maybePrime;
    }
    maybePrime++;
  }

  return prime;
}

nthPrime(20n);
//  73n

Specification
ECMAScript 2027 LanguageSpecification
# sec-bigint-objects


Web Proxy Viewer  |  New URL  |  Original Page