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?

1

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Where does the 4Gb number come from? (it does not look right)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.