-1

After testing i found an behavior not sure what everyone else does.

I have an id 002006556 its a int, but when I do the following code it changes the number, Is there a reason why?

Here is the jsFiddle

var user = 002006556;
alert(user.toString());
alert(user);

It cannot be in string because the real code come from a site platforme and the true values would var user = {Username}; The result is not what i am expecting

Jseb
  • 1,926
  • 4
  • 29
  • 51
  • If you begin a number with an 0 it is interpreted as base 8 – mousetail Jun 04 '21 at 12:48
  • So, `Username` was a string, and you're interpolating that into Javascript through some templating language? Then you need to ensure it *stays* a string to preserve its contents, preferably by having your templating language output the value JSON-encoded. – deceze Jun 04 '21 at 12:52

1 Answers1

1

This happens because it is interpreted as a base 8 number. If you start a number with 0 in javascript, it is interpreted as base 8 instead of base 10. For exaple:

012 == 10 since 1 * 8 + 2 = 10

Confusingly, this does not happen if any of the numbers are 8 or higher:

080 == 80

mousetail
  • 7,009
  • 4
  • 25
  • 45