Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 0010 in javascript is equal to 8?

i wrote from 001 to 0010 and much more digit like this that started with "00" in chrome console and Fire Fox even in IE and get this result.

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

like image 593
Vahid Mehrabi Avatar asked Apr 10 '13 12:04

Vahid Mehrabi


2 Answers

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

Thus 10 interpreted as octal is equal to 8 in decimal.

For more information refer to MDN on number literals.

like image 82
Sirko Avatar answered Oct 19 '22 04:10

Sirko


"Numeric constants are considered octal if they are preceded by a zero, and are considered hexadecimal if they are preceded by a 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 in fact an octal number and equals 8.

like image 4
David van Driessche Avatar answered Oct 19 '22 04:10

David van Driessche