In JavaScript, integers are accurate only up to 15 digits.
let x = 999999999999999; let y = 9999999999999999; |
JavaScript stores all numbers in a 64-bit floating-point format based on the IEEE 754 standard. Due to this standard, large integers may not be precisely represented and can be rounded.
JavaScript can accurately represent integers within the range of -9007199254740991-(253-1) to 9007199254740991+(253-1).
Integer values beyond this range may lose precision due to the limitations of the floating-point format.
To create a BigInt, you can either append “n” to the end of an integer or use the BigInt() function:
Example
let x = 9999999999999999; let y = 9999999999999999n; |
Example
let x = 1234567890123456789012345n; let y = BigInt(1234567890123456789012345) |
The typeof a BigInt in JavaScript is “bigint”.
let x = BigInt(999999999999999); let type = typeof x; |
BigInt is the second numeric data type in JavaScript, following Number.
With BigInt, the total number of supported data types in JavaScript reaches 8: