In JavaScript, eval(010) returns 8 [duplicate]

Because 010 is parsed as octal. Javascript treats a leading zero as indicating that the value is in base 8.

Similarly, 0x10 would give you 16, being parsed in hex.

If you want to parse a string using a specified base, use parseInt:

parseInt('010', 8); // returns 8.
parseInt('010',10); // returns 10.
parseInt('010',16); // returns 16.

Leave a Comment