Member-only story
How to handle BigInt in Javascript: The missing numbers
Just imagine, your APIs are returning a huge number like 900719925474099999, but after JSON parsing the response, you received 900719925474100000. Isn’t it weird to see something change after a simple parsing?
let x=900719925474099999;
console.log(x); // Console: 900719925474100000
let y=900719925474099999n;
console.log(y); // Console: 900719925474099999
This is going to have a huge impact on business and computation. We recently faced this situation. And appending n at the end of the number will solve this issue. But how can we expect a JSON number to have n at the end?
Let’s find out:
- What is happening here in the background?
- Why appending n at the end solving this issue?
- How have we handled it for our use case?
- What are some of the best and most practical ways to handle this situation?
Introduction
Javascript uses a common number type for integer and float types. In JavaScript, all numbers are stored in a 64-bit floating-point format (IEEE 754 standard). As a result, the number type can support a range of numbers between -(2⁵³-1) and (2⁵³-1). This range is defined by two constants Number.MIN_SAFE_INTEGER and…