In JavaScript, eval(010) returns 8

parseint javascript
javascript integer
javascript hex to decimal

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.

Why eval(010) returns 8?, This happens even if 010 isn't a string. Because 010 is parsed as octal. Javascript treats a leading zero as indicating that the value is in base 8. Similarly, 0x10  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.


Prefixing a number with an 0 means it's octal, i.e. base 8. Similar to prefixing with 0x for hexadecimal numbers (base 16).

Use the second argument of parseInt to force a base:

> parseInt('010')
8
> parseInt('010', 10)
10

Speaking JavaScript: An In-Depth Guide for Programmers, eval() Is Cleaner in Strict Mode In strict mode, the eval() function becomes less For example: > 010 === 8 true In strict mode, you get a syntax error if you use function f() { 'use strict'; return 010 } SyntaxError: Octal literals are not allowed in  The eval() function evaluates or executes an argument. If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements.


If you'd like to output the string 010 to the document, you can wrap the value in quotation marks:

var octal = eval('"010"');
typeof octal; // "string"

If you want to parse an integer or understand octals, read the other answers.

parseInt() - Explained with Examples, JavaScript's parseInt() function is used to parse a string and return an 8); //​returns 8 parseInt('010') //returns 10 parseInt('17', 8); //returns 15  If the argument of eval() is not a string, eval() returns the argument unchanged. In the following example, the String constructor is specified and eval() returns a String object rather than evaluating the string. eval(new String('2 + 2')); // returns a String object containing "2 + 2" eval('2 + 2'); // returns 4


JavaScript parseInt() Function, Note: If the first character cannot be converted to a number, parseInt() returns NaN. Note: Older browsers will result parseInt("010") as 8, because older versions  Note: Older browsers will result parseInt ("010") as 8, because older versions of ECMAScript, (older than ECMAScript 5, uses the octal radix (8) as default when the string begins with "0". As of ECMAScript 5, the default is the decimal radix (10).


JavaScript : Moving to ES2015, strictFunc(); // ReferenceError: strictVar is not defined The eval() function is zero is interpreted as octal (base 8) as follows: > 010 === 8 true In strict mode, you { 'use strict'; return 010 } //SyntaxError: Octal literals are not allowed in Running  The reason is simple: long, long time ago JavaScript was a much weaker language, many things could only be done with eval. But that time passed a decade ago. But that time passed a decade ago. Right now, there’s almost no reason to use eval .


JavaScript: Functional Programming for JavaScript Developers, strictFunc(); // ReferenceError: strictVar is not defined The eval() function is zero is interpreted as octal (base 8) as follows: > 010 === 8 true In strict mode, you { 'use strict'; return 010 } //SyntaxError: Octal literals are not allowed in Running  It may be better to return 0 or false in these situations, though, depending on the application (in JavaScript, 0 and false are the same thing): function divByZero ( dividend , divisor ) var quotient = dividend / divisor ;