-3

So my understanding is that eval() can take a string that is a mathematical expression and return the value of the expression. Why is it then that adding a '0' before a number e.g. changing '11' to '011' changes the result?

r0ss26
  • 27
  • 1
  • 1
  • 4

1 Answers1

2

Because 011 is an octal literal which is equal to decimal 9 (9 + 9 gives 18). Removing the 0 prefix makes the value decimal, so the result is decimal 11 (11 + 11 gives 22).

You can try it from a node REPL

> eval('011')
9

If you enable strict mode, it wont allow octal literals. related

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61