JavaScript BigInt Arithmetic: Exact Integer Operations
Quick answer
BigInt represents arbitrary-size signed integers subject to resource limits. It is useful for exact integer computation beyond Number’s safe range.
How it works
BigInt operators include addition, subtraction, multiplication, division, remainder and exponentiation. Division truncates toward zero; division by 0n and a negative exponent throw RangeError. Arithmetic generally requires both operands to be BigInt. Relational comparisons can compare Number and BigInt, but strict equality distinguishes their types. Return decimal strings when a JSON-only assessment cannot serialize BigInt. For fixed-width integer formats, BigInt.asIntN and BigInt.asUintN deliberately wrap to a chosen bit width; see their references rather than assuming arbitrary-size arithmetic wraps.
console.log(String(9007199254740993n + 10n));console.log(String(-7n/3n), String(-7n%3n));Expected output in Node.js:
9007199254741003-2 -1Exact integer addition and truncating division preserve integer semantics.
console.log(3n === 3, 3n < 4, String(2n ** 10n));Expected output in Node.js:
false true 1024Relational comparison permits mixed numeric types; strict equality does not.
Common mistakes
BigInt(roundedNumber) preserves the rounded value, not lost precision. Math methods generally require Number conversion, not BigInt.
Practice
Compare negative division and remainder; try converting a decimal string before adding.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript BigInt Arithmetic: Exact Integer Operations — Knowledge Check
Apply the taught concept and predict the output.
What is -9n / 2n?
Which exception results from 1n / 0n?
Which representation preserves an exact large integer in a JSON-only output contract?
What is the exact console output in JavaScript BigInt Arithmetic: Exact Integer Operations example 1?
console.log(String(9007199254740993n + 10n));console.log(String(-7n/3n), String(-7n%3n));What is the exact console output in JavaScript BigInt Arithmetic: Exact Integer Operations example 2?
console.log(3n === 3, 3n < 4, String(2n ** 10n));