Search code examples
javascriptnode.jsintegernumbers

Number in Javascript changes after declaring it as a variable. Even parsing it from a string returns a wrong number


This is very weird please check it yourself.

Whenever I try to declare variable with the number 76561198322368902 it always changes to 76561198322368900 no matter what. Chrome Devtools Console Screenshot

Even when I pass the variable as a string and later parse to a number, it always changes to 76561198322368900.

I really don't know what to do. I never thought Integers were capable of such thing.


Solution

  • The "maximum safe integer" in JavaScript is 9007199254740991 - trying to use the number type beyond this can yield unpredictable results.

    Use the BigInt type if you need a bigger number.