Glossary / BigInt

BigInt

BigInt represents integers with arbitrary precision, unlike Number’s finite precision range. Use it for integer domains that can exceed Number.MAX_SAFE_INTEGER, such as large identifiers or counters, when the surrounding APIs and serialization format support it. BigInt is not a general replacement for Number because it cannot represent fractional values and does not implicitly mix with Number.

Arithmetic and interoperability

const large = 9007199254740993n;
large + 1n; // exact integer arithmetic
// large + 1; // TypeError: cannot mix BigInt and Number

Create values with an n literal or BigInt(string/integer). Division truncates toward zero. There is no BigInt NaN or infinity. Explicitly converting a BigInt to Number can lose precision; check range first. BigInt has dedicated typed-array and DataView forms, but many APIs, JSON, and math functions expect Number.

JSON.stringify throws on a BigInt by default. Define a documented encoding—often a decimal string—or a deliberate toJSON/replacer strategy; converting to Number can corrupt data. BigInt arithmetic is not guaranteed constant-time, so it is not automatically safe for cryptographic secrets.

Design considerations

Agree on wire formats across services and languages. Validate decimal strings before conversion and account for database driver behavior. Use BigInt only when exact large-integer semantics are needed, not merely because it sounds more precise.

Interview questions

  1. Can Number and BigInt be added directly? No; convert deliberately.
  2. How does BigInt division behave? Integer division truncates toward zero.
  3. Does JSON serialize BigInt by default? No; it throws TypeError.
  4. Can BigInt be used for secret cryptography safely? Not by default; operations are not guaranteed constant-time.

References