BigInt
BigInt is a primitive for integers of arbitrary size. You write it with an n suffix (9007199254740993n) or create it with BigInt(value). It does not mix with number in arithmetic.
Detailed Explanation
JavaScript's number is an IEEE-754 double. Integers between -(2^53 − 1) and 2^53 − 1 are safe; outside that range they silently lose low bits. 9007199254740993 === 9007199254740992 is true. That is fatal for 64-bit ids, cryptography, money in minor units if you ever exceed the range, and counters that grow without bound.
BigInt stores an integer with as many bits as it needs. 2n ** 80n is exact. The type is typeof 1n === "bigint". It is a primitive, not an object, and it has its own operators: + - * / % ** and the bitwise operators except >>>. Division truncates toward zero: 5n / 2n is 2n, not 2.5n. There is no fractional BigInt.
You cannot mix BigInt and number in arithmetic. 1n + 1 throws TypeError. Convert on purpose: 1n + BigInt(1) or Number(1n) + 1. Number(huge) loses precision if the integer does not fit in a double, so convert only when you know the value is small. Comparisons (<, >, ==) do mix types: 1n == 1 is true and 1n === 1 is false. Relational operators compare the mathematical values.
BigInt("10") parses a string. BigInt(10) accepts an integer number. BigInt(10.7) throws, because a non-integer number is not a whole value. BigInt(null) throws. There is no parseBigInt; invalid strings throw SyntaxError.
JSON has no BigInt. JSON.stringify(1n) throws. Send BigInts as strings, or write a replacer / reviver pair. TypedArray and Number APIs do not take BigInt except the 64-bit typed arrays (BigInt64Array, BigUint64Array).
What it costs you: speed and friction. BigInt arithmetic is slower than number arithmetic and allocates for large values. Mixing types throws, so APIs must pick one. Library math (Math.sqrt, Math.max) does not accept BigInt. You write your own helpers when you need them.
Reach for BigInt when the integer must stay exact beyond 53 bits: database ids from Snowflake-style generators, crypto.getRandomValues widths, protocol fields that are uint64. Keep money in integer minor units as number if they fit, or as BigInt if they might not. Do not use BigInt for everyday counters.
Code Examples
console.log(9007199254740993 === 9007199254740992); // => true
console.log(9007199254740993n === 9007199254740992n); // => false
console.log(2n ** 10n); // => 1024n
console.log(5n / 2n); // => 2n
console.log(1n === 1); // => false
console.log(1n == 1); // => true
console.log(2n > 1); // => truetry {
console.log(1n + 1);
} catch (err) {
console.log(err.name); // => TypeError
}
console.log(1n + BigInt(1)); // => 2n
console.log(Number(2n) + 1); // => 3function idToJson(id) {
return JSON.stringify({ id: id.toString() });
}
console.log(idToJson(9007199254740993n));
// => {"id":"9007199254740993"}Common Gotchas
`JSON.stringify` rejects BigInt
serialise as a string (or a custom tag) and parse back with `BigInt`.
`Number(big)` is lossy for large values
check `big <= Number.MAX_SAFE_INTEGER` before converting.
`/` on BigInt truncates
`5n / 2n === 2n`. There is no remainder in the quotient; use `%` for the leftover.