2
9007199254740992 == 9007199254740991
false

9007199254740992 == 9007199254740992 
true

9007199254740992 == 9007199254740993
true  //  whats going on ?

9007199254740992 == 9007199254740994
false

9007199254740992 == 9007199254740995
false

Could someone explain the logic behind the 3rd check? Why does it return TRUE?

Snehal Masne
  • 3,403
  • 3
  • 31
  • 51
  • 3
    Javascript stores numbers as [floating point](https://en.wikipedia.org/wiki/Double-precision_floating-point_format). You can't [safely](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER) represent `9007199254740993` as an integer. Given your choice of integer (`9007199254740991 === Number.MAX_SAFE_INTEGER`) something tells me you already know this? – CodingIntrigue Sep 10 '15 at 12:02
  • I'm not sure why, but 9007199254740993 becomes 9007199254740992 when entered in the console of my browser. That would explain why you are seeing it being equal to 9007199254740992. – forgivenson Sep 10 '15 at 12:02
  • 1
    @RGraham sounds like you should put that as the answer – forgivenson Sep 10 '15 at 12:04
  • 1
    @forgivenson It's been asked hundreds of times before. I'm trying to find an appropriate duplicate – CodingIntrigue Sep 10 '15 at 12:05

1 Answers1

2

Javascript stores numbers as 64-bit floating-point numbers. These can only represent integers up to 53 bits exactly, and after that they are only approximate. The largest exact integer in JS is therefore 9 007 199 254 740 991.

Community
  • 1
  • 1
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175