Mikael Kessler
Mikael Kessler

Reputation: 1235

Javascript array returning incorrect value for item?

Does anyone know why the following would return the third item's value + 1 rather than just returning the item's correct value?

JSFiddle: http://jsfiddle.net/gz4gA/

var items = [10150778347353933, 95698693940, 10151139500303463, 337394396325739, 181838311261, 186661577450, 131239636262, 408877845803303, 130260147046659, 10151850964430652, 190553376302, 256062744215, 73099117435, 30571361895, 85358203371, 133621058800, 67899501771, 422515109312, 56271008850, 107997326726, 382312857465, 144163385597840, 10150882307310061, 184244804960462, 63953746442, 114588455254586, 201992215550, 401799715673, 287217811299764, 96668113887, 106824841365, 130084127023410, 186061480932, 60351005403, 209693813195, 10150593297330601, 319622191193, 68686268942, 290590784383, 63467856465, 470235003273, 127564720953, 127437064077, 102351319089, 113530295368, 82102942060, 290699835908, 81445359355, 137490940846, 119104781632, 415115129332, 120520318032430, 142666108227, 146711457500, 344723925588563, 239108779476988, 121853939741, 109322101963, 54456613147, 473850413696, 10150329755772674];
console.log(items[2]);

Upvotes: 2

Views: 98

Answers (2)

jfriend00
jfriend00

Reputation: 707328

Per this answer https://stackoverflow.com/a/307200/816620, the top of the range where javascript can represent all integers is: 9007199254740992 which is smaller than your 10151139500303463. So, your number is too large.

If these are something like item codes rather than something you really need to use as a number, then perhaps you should store them as strings, not as numbers.

Upvotes: 2

Ted Hopp
Ted Hopp

Reputation: 234795

JavaScript numbers are 64-bit floating point values. The largest integer value that can be represented exactly is 253, or 9007199254740992. Numbers larger than that (including the 3rd element of your array) are represented only approximately. See, for instance, ECMA 262-5, section 8.5.

Upvotes: 4

Related Questions