How to convert strings to bigInt in JavaScript

后端 未结 3 767
北海茫月
北海茫月 2021-02-05 19:25

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

Example

var reqId = \"78099864177253771992779766288266836166272662\";
var         


        
3条回答
  •  梦如初夏
    2021-02-05 19:56

    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';
    BigInt(string);
    // 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;
    

提交回复
热议问题