4

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?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • Javascript will not handle integers with more precision than that by itself. You will have to use some sort of library that creates its own numeric format and offers its own operators for it. – jfriend00 Jun 29 '16 at 04:30
  • 2
    You can store integers larger than that, just not all of them – Bergi Jun 29 '16 at 04:30
  • Relevant: [What is JavaScript's highest integer value that a Number can go to without losing precision?](http://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin) – Spencer Wieczorek Jun 29 '16 at 04:32

4 Answers4

1

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).

Anthony Astige
  • 1,919
  • 1
  • 13
  • 18
1

BigInteger.js is an arbitrary-length integer library for Javascript, allowing arithmetic operations on integers of unlimited size, notwithstanding memory and time limitations.

Ahmer Ali Ahsan
  • 5,636
  • 16
  • 44
  • 81
0

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

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
0

There are two ways we can store big int.

const bigIntVal=123243435454656565657575n;

or

const bigIntVal=BigInt("123243435454656565657575");