From what I understand integers range from
-2147483648 through 2147483647
I am confused because I noticed some references to big int in javascript. Can someone explain how I can store an integer number larger than 2147483647?
From what I understand integers range from
-2147483648 through 2147483647
I am confused because I noticed some references to big int in javascript. Can someone explain how I can store an integer number larger than 2147483647?
To get past JavaScript's internal numeric limits, use something like a bignum library (Just a random one I found, research for a good library left as an exercise to OP).
BigInteger.js is an arbitrary-length integer library for Javascript, allowing arithmetic operations on integers of unlimited size, notwithstanding memory and time limitations.
Depending on what you are doing the number, you can handle integers up to +/- (2^53)-1 or 9007199254740991
.
While not all browsers support Number.MAX_SAFE_INTEGER
yet, you can calculate it with:
Math.pow(2, 53) - 1
Note: This works when in pure math functions, but many bitwise operations are limited to 16 bit integers, as well as the index in arrays.
More details can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
There are two ways we can store big int.
const bigIntVal=123243435454656565657575n;
or
const bigIntVal=BigInt("123243435454656565657575");