5
I know that the function should not be used indiscriminately, because it has several security implications, conforms can be seen here:
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?
Related: What are Decimal, Hexadecimal and Octal notation numbers?
– rray
But in octal 023 is equal to 27 and in hexadecimal is equal to 17. I do not know if this is the case...
– José Olinda
19 octal is equal to 23 decimal :)
– MarceloBoni
023 = 2*8 + 3 = 19
. The octal base is indicated by a0
before the number, and hexadecimal is0x
– Jefferson Quesado
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.
– José Olinda
No, you still don’t understand, he reads the decimal
23
and how has the0
in front, it interprets the decimal value as octal, in case19
– MarceloBoni
@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.
– José Olinda
@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 with0
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.– Jefferson Quesado
@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.
– José Olinda