Code Logo

BigInt Arbitrary Precision

Published at25 Jul 2026
JavaScript Functions Hard 0 views
Like0

Write a JavaScript function that takes two integers as strings (may exceed Number.MAX_SAFE_INTEGER), converts them to BigInt, adds them, and returns the result as a string. BigInt provides arbitrary precision for integer arithmetic beyond the 53-bit limitation of Number.

BigInt was introduced in ES2020 to address JavaScript's limitations with large integers. The Number type can only safely represent integers up to 2^53 - 1 (about 9 quadrillion). Beyond that, precision is lost. BigInt can represent arbitrarily large integers with exact precision. BigInt literals are written with an n suffix: 9007199254740993n.

BigInt operations behave like mathematical integer arithmetic: division truncates toward zero. BigInt cannot be mixed with regular Number in operations — they must be explicitly converted. The typeof a BigInt is "bigint". BigInt supports +, -, *, /, %, **, comparison operators, and bitwise operators.

Time complexity is O(n) where n is the number of digits. Space complexity is O(n). BigInt arithmetic is implemented natively and is much faster than manual string-based big number arithmetic.

Edge cases include negative BigInts, zero, very large values with hundreds of digits, and converting the result back to string for display (use .toString() without the n suffix).

Example Input & Output

Example 1
Input
"0","0"
Output
0
Explanation

Zero addition

Example 2
Input
"-100","200"
Output
100
Explanation

Negative plus positive

Example 3
Input
"99999999999999999999","1"
Output
100000000000000000000
Explanation

Carry beyond safe range

Example 4
Input
"12345678901234567890","98765432109876543210"
Output
111111111011111111100
Explanation

Large numbers

Example 5
Input
"9007199254740993","1"
Output
9007199254740994
Explanation

Beyond safe integer, precise result

Algorithm Flow

Recommendation Algorithm Flow for BigInt Arbitrary Precision
Recommendation Algorithm Flow for BigInt Arbitrary Precision

Solution Approach

function solution(a, b) {
  var bigA = BigInt(a);
  var bigB = BigInt(b);
  return (bigA + bigB).toString();
}

Best Answers

javascript - Approach 1
function solution(a, b) {
  var bigA = BigInt(a);
  var bigB = BigInt(b);
  return (bigA + bigB).toString();
}