Why is 0010 in javascript equal to 8?

I wrote from 001 to 0010 and a lot more numbers like this, which started with β€œ00” in the Chrome and Fire Fox console even in IE and got this result.

why is 0010 not equal to 10? or why is 0020 not equal to 20? and this is "16".

+4
source share
4 answers

A leading zero indicates that the number should be interpreted as octal.

Thus, 10 , which is interpreted as octal, is equal to the decimal value of 8 .

See MDN in numeric literals for more information.

+6
source

The number of literals in Javascript can be entered in different databases -

a leading zero means that the number is a number in octal base (only digits 0-7), so 010 is the same as: once 8 + zero

the literal 0x10 is in hexadecimal (base 16), so it is: once 16 + zero) = 16

see here https://developer.mozilla.org/en/docs/JavaScript/Guide/Values,_variables,_and_literals

+3
source

"Numeric constants are considered octal if preceded by zero, and are considered hexadecimal if they are preceded by zero and and x (0x)." (as explained here )

008 is not considered octal because it contains "8", which is not an octal number. 0010 is actually an octal number and equal to 8.

+3
source

Because the leading 0 is the Octal number system. Similarly, if you typed 0x010, it will be 16, since 0x is a prefix for the Hexadecimal system.

+3
source

All Articles