Javascript: why does Eval("023") return 19?

Asked

Viewed 56 times

5

I know that the function should not be used indiscriminately, because it has several security implications, conforms can be seen here:

Eval is either good or bad?

But I was making a simple calculator, where the text of the input, after treated, is passed to the Eval function and returns the result. However, when inserting 023 and pressing equal (equivalent to passing the string in the input to the Eval function) I was surprised by the result 19.

I opened the Chrome console and entered the code:

eval("023")

And I got the following result:

19

Why the execution result of the above row is 19?

Veja o código em execução

  • But in octal 023 is equal to 27 and in hexadecimal is equal to 17. I do not know if this is the case...

  • 19 octal is equal to 23 decimal :)

  • 023 = 2*8 + 3 = 19. The octal base is indicated by a 0 before the number, and hexadecimal is 0x

  • Alright guys, I waver mine here. I did not notice that the parser of Eval was interpreting in octal and returning in decimal, I had understood the opposite. Valew o help @marceloboni e demais q commented.

  • 1

    No, you still don’t understand, he reads the decimal 23 and how has the 0 in front, it interprets the decimal value as octal, in case 19

  • @Marceloboni I understood yes, you who did not understand well what I said. The return is in decimal, since there is no digit 9 in octal.

  • 1

    @Joséolinda, see my comment and also see the @rray link. If it’s a number we start with 0x, then it’s hexadecial, if you start with 0 then it is octal, and only otherwise it is considered decimal. Also worth remembering that the standard representation/print representation of a number is as decimal.

  • 2

    @Jeffersonquesado already noted yes, thanks for the tips. There was an astonishment at first with the return and I didn’t notice, but now everything is clear. Thank you for participating.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.