JavaScript BigInt for Exact Integer Arithmetic
Quick answer
BigInt is a primitive integer type for values that can exceed the safe Number range. It is not a decimal or floating-point type. BigInt arithmetic is subject to available memory and computational resources.
How it works
Write an integer literal followed by n, or use BigInt with a valid integer string. BigInt division truncates toward zero. Arithmetic such as addition requires both operands to be BigInt when using BigInt arithmetic; mixing BigInt and Number for + with two numeric operands throws TypeError. Convert to String when you need an exact textual representation.
const huge = 9007199254740993n;console.log(String(huge + 2n));Expected output in Node.js:
9007199254740995No conversion through Number occurs.
console.log(String(7n / 2n));console.log(String(-7n / 2n));Expected output in Node.js:
3-3Both results truncate toward zero.
Common mistakes
1n + 1 is an intentionally failing numeric expression. Avoid BigInt(largeNumber) when the Number may already have lost precision. The default JSON serializer does not serialize BigInt values without customization; return a string for this exercise.
Practice
Accept a decimal integer string and return the next integer as a string, preserving its exact value.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript BigInt for Exact Integer Arithmetic — Knowledge Check
Apply the taught concept and reason about the result.
Which literal denotes a BigInt?
What does String(-9n / 2n) produce?
What happens when evaluating 2n + 3?
Why prefer BigInt("9007199254740993") to BigInt(9007199254740993)?
How can a large BigInt result be represented in a default JSON-compatible exercise?