The question doesn't make sense. What does it mean that a number "goes to" an integer value? If you just want to ask what is the highest integer you can represent in JS, the highest (finite) Number itself is an integer.
– VekyOct 26 '18 at 3:04

Technical note on the subject of the number 9007199254740992: There is an exact IEEE-754 representation of this value, and you can assign and read this value from a variable, so for very carefully chosen applications in the domain of integers less than or equal to this value, you could treat this as a maximum value.

In the general case, you must treat this IEEE-754 value as inexact, because it is ambiguous whether it is encoding the logical value 9007199254740992 or 9007199254740993.

So what's the smallest and largest integer we can use to assure exact precision?
– PacerierOct 15 '11 at 16:21

37

Maybe worth noting that there is no actual (int) in javascript. Every instance of Number is (float) or NaN.
– Beetroot-BeetrootAug 31 '12 at 13:09

50

9007199254740992 is not really the maximum value, the last bit here is already assumed to be zero and so you have lost 1 bit of precision. The real safe number is 9007199254740991 ( Number.MAX_SAFE_INTEGER )
– Willem D'HaeseleerAug 21 '14 at 17:59

This is the maximum floating point value. The question is about the highest integer value. And while Number.MAX_VALUE is an integer, you can't go past 2^53 without losing precision.
– TeepeemmJul 22 '14 at 22:01

Jimmy's answer correctly represents the continuous JavaScript integer spectrum as -9007199254740992 to 9007199254740992 inclusive (sorry 9007199254740993, you might think you are 9007199254740993, but you are wrong!
Demonstration below or in jsfiddle).

document.write(9007199254740993);

However, there is no answer that finds/proves this programatically (other than the one CoolAJ86 alluded to in his answer that would finish in 28.56 years ;), so here's a slightly more efficient way to do that (to be precise, it's more efficient by about 28.559999999968312 years :), along with a test fiddle:

The answer "To be safe: var MAX_INT = 4294967295;" isn't humorous. If you're not bitshifting, don't worry about it (unless you need an int larger than 4294967295, in which case you should probably store it as a string and use a bigint library).
– CoolAJ86Dec 27 '14 at 19:43

If you’re using bitwise operators anywhere (or if you’re referring to the length of an Array), the ranges are:

Unsigned: 0…(-1>>>0)

Signed: (-(-1>>>1)-1)…(-1>>>1)

(It so happens that the bitwise operators and the maximum length of an array are restricted to 32-bit integers.)

If you’re not using bitwise operators or working with array lengths:

Signed: (-Math.pow(2,53))…(+Math.pow(2,53))

These limitations are imposed by the internal representation of the “Number” type, which generally corresponds to IEEE 754 double-precision floating-point representation. (Note that unlike typical signed integers, the magnitude of the negative limit is the same as the magnitude of the positive limit, due to characteristics of the internal representation, which actually includes a negative 0!)

On the side before the arrow, we have bit value 1, and an adjacent radix point, then by multiplying 2^52, we right move the radix point 52 steps, and it goes to the end. Now we get 4503599627370496 in binary.

Now we start to accumulate 1 to this value until all the bits are set to 1, which equals 9 007 199 254 740 991 in decimal.

Now, because that in double-precision 64-bit binary format, it strictly allots 52 bits for fraction, no more bit is available to carry for adding one more 1, so what we can do is setting all bits back to 0, and manipulate the exponent part:

Bitwise operations are working on 32 bits on Javascript.
– VjeuxMay 4 '11 at 14:37

1

@danorton: I'm not sure you understand what you are doing. ^means raised to the power. In the javascript console, ^ is XOR, not raised-to
– kumar_harshDec 24 '13 at 11:09

1

@Kumar, I don’t understand your meaning or how any other meaning of “^” is relevant here in this question about JavaScript.
– danortonDec 25 '13 at 14:42

3

Again, I am not at all confused. I have commented and downvoted on what is written. If Math.pow() is what is meant, then that is what should be written. In an answer to a question about JavaScript, it is inappropriate to use syntax of a different language. It is even more inappropriate to use a syntax that is valid in JavaScript, but with an interpretation in JavaScript that has a different meaning than what is intended.
– danortonDec 31 '13 at 18:56

9

2^31 is how one writes two to the thirty-first power in English. It's not in a code block. Would you complain about someone using a ; in an answer, because that's a character with a different meaning in Javascript?
– lmmMar 5 '14 at 13:55

Description

The MAX_SAFE_INTEGER constant has a value of 9007199254740991 (9,007,199,254,740,991 or ~9 quadrillion). The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(253 - 1) and 253 - 1.

Safe in this context refers to the ability to represent integers exactly and to correctly compare them. For example, Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 will evaluate to true, which is mathematically incorrect. See Number.isSafeInteger() for more information.

Because MAX_SAFE_INTEGER is a static property of Number, you always use it as Number.MAX_SAFE_INTEGER, rather than as a property of a Number object you created.

Browser compatibility

At the moment of writing, JavaScript is receiving a new data type: BigInt. It is a TC39 proposal at stage 3. BigInt is available in Chrome 67+, FireFox 67+ (needs an option to activate it), Opera 54 and Node 10.4.0. It is underway in Safari, et al... It introduces numerical literals having an "n" suffix and allows for arbitrary precision:

var a = 123456789012345678901012345678901n;

Precision will still be lost, of course, when such a number is (maybe unintentionally) coerced to a number data type.

anything you want to use for bitwise operations must be between
0x80000000 (-2147483648 or -2^31) and 0x7fffffff (2147483647 or 2^31 -
1).

the console will tell you that 0x80000000 equals +2147483648, but
0x80000000 & 0x80000000 equals -2147483648

Hex-Decimals are unsigned positive values, so 0x80000000 = 2147483648 - thats mathematically correct. If you want to make it a signed value you have to right shift: 0x80000000 >> 0 = -2147483648. You can write 1 << 31 instead, too.

Since no one seems to have said so, in the v8 engine there is a difference in behavior for 31 bits number and number above that.

If you have 32 bits you can use the first bit to tell the javascript engine what type that data is and have the remaining bits contain the actual data. That's what V8 does as a small optimisation for 31 bisnumbers (or used to do, my sources are quite dated). You have the last 31 bits being the number value and then the first bit telling the engine if it's a number or an object reference.

However if you use number above 31 bits then the data won't fit in, the number will be boxed in 64 bits double and the optimisation won't be there.

The Bottom line, in the video below, is:

prefer numeric values that can be represented as 31bits signed
integers.

Basically javascript doesn't support long.
so for normal values that it can represent less then 32 bit, it will use the int type container. for integer values greater then 32 bit its uses double. In double represntation the integer part is 53 bit and rest is mantissa( to keep floating point information).
so You can use 2^53 - 1 which value is 9007199254740991
you can access the value to use in your code by Number.MAX_SAFE_INTEGER

that's maximum of a floating point value. It doesn't mean that you can store an int that long
– phuclvAug 4 '13 at 10:30

1

Or more to the point, you can't reliably store an int that long without loss of accuracy. 2^53 is referred to as MAX_SAFE_INT because above that point the values become approximations, in the same way fractions are.
– IMSoPJun 16 '14 at 18:32

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).