Convert a BigInt to a Number in JavaScript
Apr 25, 2023
BigInts are a new primitive type in JavaScript that can contain arbitrarily large integers.
You can convert a BigInt to its corresponding number representation using the Number()
function as follows:
const answerBigInt = 42n;
const answerNum = Number(answerBigInt);
typeof answerNum; // 'number'
answerNum === 42; // true
Keep in mind that, if the BigInt is larger than Number.MAX_VALUE
or smaller than Number.MIN_VALUE
, Number(bigint)
will be positive or negative infinity.
const tooBig = Number(10n**10000n);
tooBig === Number.POSITIVE_INFINITY; // true
tooBig; // Infinity
const tooSmall = Number((-10n)**10001n);
tooSmall === Number.NEGATIVE_INFINITY; // true
tooSmall; // -Infinity
Did you find this tutorial useful? Say thanks by starring our repo on GitHub!