The same operators that can be applied to a JavaScript Number can also be used with a BigInt.
let x = 9007199254740995n; let y = 9007199254740995n; let z = x * y; |
Note Arithmetic operations between a BigInt and a Number aren’t permitted due to potential loss of information during type conversion. Unsigned right shift (>>>) cannot be performed on a BigInt because BigInts do not have a fixed width. |
A BigInt cannot contain decimal fractions.
let x = 5n; let y = x / 2; // Error: Cannot mix BigInt and other types, use explicit conversion. |
let x = 5n; let y = Number(x) / 2; |
BigInt can alternatively be expressed using hexadecimal, octal, or binary notation.
let hex = 0x20000000000003n; let oct = 0o400000000000000003n; let bin = 0b100000000000000000000000000000000000000000000000000011n; |
Rounding could potentially compromise the security of a program.
9007199254740992 === 9007199254740993; // is true !!! |
BigInt has been compatible with all browsers since September 2020.
ES6 introduced the MAX_SAFE_INTEGER and MIN_SAFE_INTEGER properties to the Number object.
let x = Number.MAX_SAFE_INTEGER; |
let x = Number.MIN_SAFE_INTEGER; |
ES6 also introduced two new methods to the Number object.
If the argument passed to it is an integer, the Number.isInteger() method returns true.
Number.isInteger(10); Number.isInteger(10.5); |
A safe integer refers to an integer that can be accurately represented as a double-precision number.
The Number.isSafeInteger() method returns true if the argument provided is a safe integer.
Number.isSafeInteger(10); Number.isSafeInteger(12345678901234567890); |
Safe integers encompass all integers ranging from -(253 – 1) to +(253 – 1). An example of a safe integer is 9007199254740991, while 9007199254740992 exceeds the safe integer range. |