0

Possible Duplicate:
What is JavaScript's Max Int? What's the highest Integer value a Number can go to without losing precision?

What is the maximum integer in javascript? I have a variable that starts in 0 and adds 100 each 0.1 seconds. What is the maximum number it can reach?

BTW, I thought this question had been answered before but I couldn't find it. If it is answered, please send me a link to it =) thanks!

Community
  • 1
  • 1
jpatiaga
  • 352
  • 3
  • 10

3 Answers3

2

JavaScript numbers are IEE 794 floating point double-precision values. There's a 53-bit mantissa (from memory), so that's pretty much the limit.

Now there are times when JavaScript semantics call for numbers to be cast to a 32-bit integer value, like array indexing and bitwise operators.

Pointy
  • 405,095
  • 59
  • 585
  • 614
2

A javascript variable can have any value you like. If native support isn't sufficient, there are various libraries that provide support for unlimited precision arithmetic (e.g. BigInt.js).

The largest value for the ECMAScript Number Type is +ve infinity (but infinity isn't a number, it's a concept). The largest numeric value is given by Number.MAX_VALUE, which is just the maximum value representable by an IEEE 754 64-bit double-precision number.

Some quirks:

var x = Number.MAX_VALUE;
var y = x - 1;
var z = x - 2;

x == y; // true
x == z; // false

The range of contiguous integers representable in ECMAScript is from -2^53 to +2^53. The largest exponent is 2^1023.

RobG
  • 142,382
  • 31
  • 172
  • 209
1

It is 1.7976931348623157e+308

to try it yourself code it

CODE

alert(Number.MAX_VALUE);

http://jsfiddle.net/XHcZx/

Ashirvad
  • 2,367
  • 1
  • 16
  • 20
  • That's not what was asked, I don't think. The question explicitly says **integer**, and I suspect that means the OP is interested in the ability to represent a continuous sequence of integers. If you subtract 1 from that value, you won't really get that number minus 1. – Pointy Sep 03 '12 at 22:46
  • 1
    That's great, thanks. I wasn't aware of the Number.MAX_VALUE property, which in fact shows what is supposed to be the largest possible number in javascript. – jpatiaga Sep 03 '12 at 22:48
  • 2
    @jpatiaga you should be clear in your question - the words **integer** and **number** mean different things! – Pointy Sep 03 '12 at 22:48
  • 1
    (Though admittedly, that value is an integer :-) – Pointy Sep 03 '12 at 22:59
  • According to [ECMA-262 §15.7.3.2](http://ecma-international.org/ecma-262/5.1/#sec-15.7.3.2), that is an **approximation** of the `largest positive finite value of the Number type`. You can't construct a continuous sequence of numbers from zero, e.g. `Number.MAX_VALUE - 1 == Number.MAX_VALUE // true`. – RobG Sep 03 '12 at 23:05
  • @Pointy you're right. I needed to know about **integers**, so Number.MAX_VALUE is not the right answer. – jpatiaga Sep 04 '12 at 19:24