How to Add Big Numbers in JavaScript
A comprehensive guide to adding and handling large integers in JavaScript, covering BigInt, precision tricks, formatting, and practical workflows for reliable code in 2026.

In JavaScript, use BigInt for exact integer arithmetic beyond Number.MAX_SAFE_INTEGER. For decimal precision, prefer scaling or a decimal library, and always format results for display with Intl.NumberFormat. This guide provides practical patterns, code samples, and pitfalls to avoid when adding big numbers in JavaScript.
How to add big numbers in javascript: a practical overview
Understanding how to add big numbers in javascript begins with recognizing the limits of the built-in Number type and the availability of BigInt for exact integer arithmetic. In 2026, developers frequently reach beyond Number.MAX_SAFE_INTEGER, so it’s essential to know when to switch to BigInt and how to display results accurately. This section shows the core concepts and a few real-world examples to illustrate precision pitfalls and correct usage.
// Safe range for a regular Number
const a = 9007199254740991; // Number.MAX_SAFE_INTEGER
const b = 1;
console.log(a + b); // 9007199254740992 (precise here)
console.log(a + 2); // 9007199254740993 (may look correct but can be imprecise for larger ops)
// Use BigInt for exact integer arithmetic beyond the safe range
const A = 9007199254740991n;
const B = 1n;
console.log(A + B); // 9007199254740992nWhy this matters: When your numbers exceed the safe integer range, Number loses precision. BigInt preserves exact values, which is critical for financial calculations, unique identifiers, or cryptographic-style workflows. In practice, many projects use BigInt alongside Number, but you must keep them separate to avoid type errors.
Using BigInt for exact integers
BigInt is the primary tool for exact arithmetic with integers that exceed the safe range of Number. You can declare BigInt with a trailing n or by calling the BigInt constructor. This ensures arithmetic remains precise regardless of magnitude.
const large1 = 1234567890123456789012345678901234567890n;
const large2 = 9876543210987654321098765432109876543210n;
console.log(large1 + large2); // 11111111101111111110111111111111111111100nconst s = "1234567890123456789012345678901234567890";
const bigFromString = BigInt(s);
console.log(bigFromString); // 1234567890123456789012345678901234567890nNotes: BigInt literals end with n, and BigInt accepts numeric strings as input. When performing calculations, keep BigInt operands as BigInt; mixing with Number requires explicit conversion.
Mixing Number and BigInt: rules and gotchas
JavaScript does not allow mixing Number and BigInt in arithmetic operations. Attempting 1 + 2n results in a TypeError. You must convert values to the same type before combining them.
// Incorrect: will throw
// console.log(1 + 2n);
// Correct: convert to the same type
const sum1 = Number(1) + Number(2); // 3 (Number)
console.log(sum1);
const sum2 = 1n + 2n; // 3n (BigInt)
console.log(sum2);Recommendation: If you’re combining big numbers, prefer BigInt throughout a calculation. If you must mix types for interfacing with existing Number-based APIs, convert explicitly and carefully.
Decimal arithmetic: precision strategies for non-integers
JavaScript numbers are floating-point and can introduce rounding errors (0.1 + 0.2 !== 0.3). For monetary values or precise decimals, use scaling or a dedicated decimal library. A simple scaling pattern uses integers under the hood and divides back at the end.
function addFixed(a, b, scale = 100) {
return Math.round((a * scale) + (b * scale)) / scale;
}
console.log(addFixed(0.1, 0.2)); // 0.3If you need arbitrary precision decimals, consider libraries like decimal.js or big.js, which provide more robust decimal arithmetic without reimplementing the wheel.
Formatting and displaying large numbers nicely
Displaying large numbers in a human-friendly way is common in UIs. Intl.NumberFormat works well with Number, and modern engines support BigInt formatting too. Choose locale-aware formatting to ensure readability across users.
const n = 12345678901234567890n;
console.log(n.toLocaleString('en-US')); // 12,345,678,901,234,567,890
const f = 1234567890.1234;
console.log(new Intl.NumberFormat('en-US', { maximumFractionDigits: 2 }).format(f)); // 1,234,567,890.12Note: Some environments may require converting BigInt to string before certain formatting operations.
End-to-end example: add and display two user-provided big numbers
This example demonstrates parsing user input as strings, converting to BigInt, adding, and presenting the result as a string for display.
function sumInputs(aStr, bStr) {
const a = BigInt(aStr);
const b = BigInt(bStr);
return (a + b).toString();
}
console.log(sumInputs("1234567890123456789012345678901234567890", "9876543210987654321098765432109876543210"));
// "11111111100111111111111111111111111111100"This pattern avoids precision loss and keeps UI formatting straightforward.
Performance considerations and best practices
BigInt arithmetic is generally slower than Number, especially in loops or tight performance paths. Use BigInt when you truly need exact integer arithmetic beyond safe Number ranges, and reserve Number for ordinary, performance-critical calculations. In performance-sensitive code, profile how much BigInt affects throughput and consider alternatives for non-critical paths.
function benchmarkBigInt(iterations) {
const a = 123456789012345678901234567890n;
const b = 987654321098765432109876543210n;
let acc = 0n;
for (let i = 0; i < iterations; i++) {
acc += a + b;
}
return acc;
}
console.log(benchmarkBigInt(1000));When possible, batch BigInt work or offload to Web Workers to keep UI responsive.
Steps
Estimated time: 25-40 minutes
- 1
Assess numeric requirements
Determine whether your task requires exact integer arithmetic beyond the safe range of Number or whether normal floating-point numbers suffice. Decide if BigInt is needed and plan how to display results.
Tip: Start by outlining inputs and expected outputs to avoid type pitfalls. - 2
Choose the right type
If you expect numbers beyond 2^53-1, plan to use BigInt for calculations. For decimals, consider scaling or a decimal library.
Tip: Keep a clear boundary between BigInt and Number arithmetic. - 3
Parse and convert
Parse string inputs as BigInt when necessary and convert between Number and BigInt explicitly to avoid runtime errors.
Tip: Use BigInt(s) or BigInt('...') for reliable conversion. - 4
Perform arithmetic
Perform addition, subtraction, and multiplication with consistent types (BigInt with BigInt, Number with Number).
Tip: Avoid implicit type coercion between BigInt and Number. - 5
Format for display
Format large results with toLocaleString or Intl.NumberFormat for user-friendly presentation.
Tip: Consider locale-specific formatting for clarity. - 6
Test and validate
Create tests covering edge values like MAX_SAFE_INTEGER, very large BigInt values, and decimal edge cases.
Tip: Automated tests help catch precision regressions.
Prerequisites
Required
- Required
- Familiarity with JavaScript primitives and BigInt (ES2020+)Required
- Required
- Basic command-line knowledgeRequired
Keyboard Shortcuts
| Action | Shortcut |
|---|---|
| CopyCopies the selected code or text | Ctrl+C |
| PastePastes from clipboard into editor | Ctrl+V |
| Select AllSelects entire document or block | Ctrl+A |
| Comment/UncommentToggle line comments in many editors | Ctrl+/ |
| Run in NodeExecute a snippet or file in Node.js | Ctrl+⇧+P, then select 'Run Node Script' |
Questions & Answers
What is the maximum safe integer in JavaScript?
JavaScript's Number type can reliably represent integers up to Number.MAX_SAFE_INTEGER (2^53-1). Beyond this, precision can be lost. BigInt provides exact arithmetic for larger integers.
The largest reliably precise integer in JavaScript Number type is Number.MAX_SAFE_INTEGER; beyond that, use BigInt for exact arithmetic.
When should I use BigInt in my code?
Use BigInt whenever you need exact integer arithmetic for numbers larger than Number.MAX_SAFE_INTEGER or when exact counting is critical. For typical floating point money, consider scaling or a decimal library instead.
Use BigInt for exact integers beyond 2 to the 53rd power, or when precise counting is essential.
Can BigInt be serialized to JSON?
BigInt values cannot be serialized to JSON directly; attempting JSON.stringify on a BigInt will throw. Convert to string before serializing or implement a custom serializer.
BigInt can't be serialized to JSON directly; convert to string if you need to send or store the value.
Are BigInt operations supported in all environments?
Most modern runtimes support BigInt, including recent Node.js and major browsers. Always verify compatibility on targeted platforms and consider polyfills or fallbacks if needed.
BigInt is widely supported in modern environments, but check your targets before relying on it everywhere.
Can I mix BigInt with decimal numbers directly?
No. Mixing BigInt and Number in arithmetic is not allowed. Convert values explicitly to a common type (either all BigInt or all Number) to avoid runtime errors.
BigInt and Number can't be mixed in arithmetic; convert so both operands are the same type.
What to Remember
- Use BigInt for integers beyond safe range
- Avoid mixing BigInt and Number in calculations
- Use scaling or a library for decimal precision
- Format large numbers with Intl.NumberFormat for display
- Profile performance when using BigInt in loops