4

I don't understand why the code below returns seemingly wrong values (150 instead of 100):

var price = {
  33427009000001024: 100,
  33427009000001025: 150,
  33427010000001026: 200
};
alert(price[33427009000001024] + "," + price["33427009000001024"]);

Displayed values: 150,150

I fixed it by enclosing object properties in quotes:

var price = {
  "33427009000001024": 100,
  "33427009000001025": 150,
  "33427010000001026": 200
};

But I don't understand if quotes are really needed/required, and why I don't get an error but just wrong values?

AbyxDev
  • 1,363
  • 16
  • 30
Kon Rad
  • 174
  • 2
  • 10
  • 3
    Possible duplicate of [biggest integer that can be stored in a double](https://stackoverflow.com/questions/1848700/biggest-integer-that-can-be-stored-in-a-double) – Joshua Aug 25 '18 at 02:18

2 Answers2

4

your integer value is greater than Integer Max-Value

so, it is rounded off to the same values

33427009000001024 == 33427009000001025 // outputs true
33427009000001024 === 33427009000001025 // outputs true
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
1

Numbers have limited precision in JavaScript (well, any other language too). So in this case 33427009000001025 is rounded to 33427009000001024 and overrides the original value 100 with 150.

Putting this into string makes it precise as it's basically string and it can contain any number of characters (up to 4GB).

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43