Understanding implicit conversion in Javascript

I was looking for a pesky JavaScript error when I was passing one argument, but when it was received, it was completely different. I fixed this, but would like to know what happens in the future.

What I had to pass as an argument is "0616" (with quotes). What I actually passed was 0616 (no quotes).

So when it was received some implicit numeric conversion happened and it was received as 398. I understand the implicit and explicit conversion, but WHAT was going on to turn 0616 into 398. At the beginning of zero there seems to be something to do this is because the other values ​​I passed were nonzero in the most significant digit, preserved just fine. Is it just the ones that start from scratch?

But what is the relationship between 398 and '0616'?

Any ideas?

+3


source to share


3 answers


the reason is that the leading zero is the javascript notation for base octal, for example. 010 = 8. The notation for a hexadecimal number is the leading 0x, for example. 0x10 = 16



+2


source


Ah the magic world of javascript !!

Any numeric literal starting with 0 is treated as an octal number.



Hacked workaround

parseInt('0616', 10)

      

+5


source


0616

is the format of the old octal number. This should be the case in the new specification 0o616

, but the old format is still supported by browsers.

See wiki page :

the 0o prefix was introduced for .... and it must be supported by ECMAScript 6 (the 0 prefix was discouraged in ECMAScript 3 and dropped in ECMAScript 5).

+3


source







All Articles