Search code examples
javascriptbigintecma262

How to convert BigInt to Number in JavaScript?


I found myself in the situation where I wanted to convert a BigInt value to a Number value. Knowing that my value is a safe integer, how can I convert it?


Solution

  • Turns out it's as easy as passing it to the Number constructor:

    const myBigInt = BigInt(10);  // `10n` also works
    const myNumber = Number(myBigInt);
    

    Of course, you should bear in mind that your BigInt value must be within [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER] for the conversion to work properly, as stated in the question.