JavaScript BigInt
- Previous Page JS Number
- Next Page JS Number Methods
JavaScript BigInt variables are used to store large integer values that are too large to be represented by ordinary JavaScript numbers.
JavaScript integer precision
JavaScript integers can only be precise up to 15 digits:
Integer precision
let x = 999999999999999; let y = 9999999999999999;
In JavaScript, all numbers are stored in 64-bit floating-point format (IEEE 754 standard).
According to this standard, large integers cannot be represented accurately and will be rounded.
Therefore, JavaScript can only safely represent integers within the following range:
- Maximum is 9007199254740991(253-1)
- Minimum is -9007199254740991(-253-1)
Integer values outside this range will lose precision.
How to create BigInt
To create a BigInt, you can add n
, or call BigInt()
Function:
Example 1
let x = 9999999999999999; let y = 9999999999999999n;
Example 2
let x = 1234567890123456789012345n; let y = BigInt(1234567890123456789012345);
BigInt: A new JavaScript data type
The JavaScript type of BigInt is "bigint
:
Example
let x = BigInt(999999999999999); let type = typeof x;
BigInt is the second numeric data type in JavaScript (after Number).
For BigInt, JavaScript supports a total of 8 data types:
- String
- Number
- Bigint
- Boolean
- Undefined
- Null
- Symbol
- Object
BigInt operators
Operators that can be used with JavaScript Number can also be used with BigInt.
BigInt multiplication example
let x = 9007199254740995n; let y = 9007199254740995n; let z = x * y;
Note
Arithmetic operations (type conversion will lose information) are not allowed between BigInt and Number.
BigInt cannot perform unsigned right shift operations (>>>), as it does not have a fixed width.
BigInt decimals
BigInt cannot have decimals.
BigInt division instance
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 hexadecimal, octal, and binary
BigInt can also be written in hexadecimal, octal, or binary notation:
BigInt hexadecimal instance
let hex = 0x20000000000003n; let oct = 0o400000000000000003n; let bin = 0b100000000000000000000000000000000000000000000000000011n;
Precision Curiosity
Rounding may compromise program security:
MAX_SAFE_INTEGER instance
9007199254740992 === 9007199254740993; // Returns true!!!
Browser support
Since September 2020, all browsers support BigInt:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 67 | Edge 79 | Firefox 68 | Safari 14 | Opera 54 |
May 2018 | January 2020 | July 2019 | September 2020 | June 2018 |
Minimum and maximum safe integers
ES6 adds max and min properties to the Number object:
MAX_SAFE_INTEGER
MIN_SAFE_INTEGER
MAX_SAFE_INTEGER instance
let x = Number.MAX_SAFE_INTEGER;
MIN_SAFE_INTEGER instance
let x = Number.MIN_SAFE_INTEGER;
New number methods
ES6 also adds two new methods to the Number object:
Number.isInteger()
Number.isSafeInteger()
Number.isInteger() Method
If the parameter is an integer, the Number.isInteger() method returns true
.
isInteger() Example
Number.isInteger(10); Number.isInteger(10.5);
Number.isSafeInteger() Method
Safe integers are integers that can be precisely represented as double-precision numbers.
If the parameter is a safe integer, the Number.isSafeInteger() method returns true
.
isSafeInteger() Example
Number.isSafeInteger(10); Number.isSafeInteger(12345678901234567890);
Safe integers are from -(253 to + (253 All integers of -1) .
This is a safe integer: 9007199254740991. This is not a safe integer: 9007199254740992.
- Previous Page JS Number
- Next Page JS Number Methods