The code
console.log(791991727573303311)
Expected result 791991727573303311
791991727573303311
Actual result791991727573303300
791991727573303300
How do i solve this issue without resorting to using strings.
And
Does this only affect printing the values?
The issue is that you're going over the Number.MAX_SAFE_INTEGER constant. Which is basically the limit to which a Number is accurate, anything above that and javascript will start to round the number as in your example.
To solve this you can use BigInt which supports any size of number.
Additional information can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
2.1m questions
2.1m answers
60 comments
57.0k users