How to convert strings to bigInt in JavaScript

I need to convert a string to BigInt like BigInteger in Javascript


var reqId = "78099864177253771992779766288266836166272662";
var result = parseInt(reqId);

Resultant value not matching since JavaScript only allows integers up to (2^53)-1.

Is there any way to overcome this?

BigInt is now a native JavaScript language feature. It’s at Stage 3 in the TC39 process and it’s shipping in V8 v6.7 and Chrome 67.

To turn a string containing a valid numeric literal into a BigInt, use the global BigInt function:

const string = '78099864177253771992779766288266836166272662';
// 78099864177253771992779766288266836166272662n

If you just want to hardcode the numeric value into your code, there is no need to convert from a string; use a BigInt literal instead:

const value = 78099864177253771992779766288266836166272662n;

You can use a JavaScript lib called BigInteger.js for the is an arbitrary-length integer library for Javascript, allows arithmetic operations on integers of unlimited size, notwithstanding memory and time limitations.This lib can be download from this link.Like var largeNumber = bigInt("75643564363473453456342378564387956906736546456235345");
You can find documentation of lib here

You can use mathjs:

var reqId = "78099864177253771992779766288266836166272662";
var myBigNumber = math.bignumber(reqId);
var res = math.add(myBigNumber, 1);
// 7.8099864177253771992779766288266836166272662e+43
// 7.8099864177253771992779766288266836166272663e+43

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   How to document a function returned by a function using JSDoc

Similar Posts