BigInt arithmetic and conversions
bigint + bigint
BigInt(value)
Number(bigint)BigInt arithmetic stays in BigInt and truncates division toward zero. Mixing in Number for +, -, *, /, %, **, or bitwise operators throws TypeError.
| Parameter | Description |
|---|---|
value | BigInt() accepts an integer, an integer-like string, or another BigInt. A non-integer Number such as 1.5 throws RangeError. |
Returns: A BigInt, except Number(bigint), which returns a Number and may lose precision for large values.
Throws
TypeError— Thrown when a BigInt and a Number are combined in arithmetic or bitwise operations.RangeError— Thrown by BigInt() when the value is not an integer, for example BigInt(1.5).
const n = 10n;
console.log(typeof n, n + 2n, 5n / 2n);
try {
console.log(1n + 1);
} catch (error) {
console.log(error.name);
}
console.log(1n == 1, 1n === 1, 1n > 0);
try {
BigInt(1.5);
} catch (error) {
console.log(error.name);
}
console.log(Number(1n), BigInt("10"));Expected output
bigint 12n 2n
TypeError
true false true
RangeError
1 10nRelated: JSON.stringify and BigInt, Number and Math