Large integer arithmetic
Calculating exact integer values beyond Number’s safe precision range.
JS Puzzles · Const
BigInt is JavaScript’s integer type for values of arbitrary length. Its arithmetic resembles Number arithmetic, but operands cannot be mixed with regular numbers and division truncates toward zero.
BigInt values represent integers of arbitrary length. Create them with an n suffix or BigInt(), then use matching BigInt operands for arithmetic; division rounds toward zero and produces another BigInt.
BigInt(integerOrString)
integerLiteralnArithmetic expressions involving BigInts return BigInt values.
const total = 2n + 3n;
const quotient = 5n / 2n;
console.log(total); // 5n
console.log(quotient); // 2nBigInt operands support arithmetic, and division discards the fractional part by rounding toward zero.
Calculating exact integer values beyond Number’s safe precision range.
Performing integer division where fractional results are intentionally discarded.
Representing large integer quantities received as strings or other supported inputs.
BigInt is a numeric type for integers of arbitrary length. It can be created with an integer literal followed by n or with BigInt().
Follow-up: How does BigInt division differ from Number division? It rounds toward zero and removes the fractional part.
Arithmetic operands must use the same numeric type, so a BigInt cannot be directly combined with a regular Number. Explicitly convert with BigInt() or Number() when mixing is required.
Follow-up: Why is explicit conversion preferred? It makes the intended result type clear and avoids implicit precision-related surprises.
BigInt arithmetic returns BigInt values, including division. Division rounds toward zero, so 5n / 2n evaluates to 2n rather than a fractional value.
Follow-up: What happens when converting the result to Number()? The value becomes a Number, and large integers can lose precision.
BigInt()Number()divisionunary plus operatorcomparisons