eval(0+'1'+3) => 11 (???)
When eval(0+'1') is executed =>1. Iam expecting that 0+'1' will give me 1 & 3 will be considered as string & o/p => 13. But, why is that not happening?
whereas
eval(1+'1'+3) => 113
You are creating the string "013", which is evaluated as a JavaScript integer literal. Integer literals starting with 0 are interpreted base 8 (octal), so your number is 8 + 3, which is 11.
Only integer literals starting with a non-zero digit are interpreted base 10.
You are expecting that concatenating the number 0 and the string '1' would be concatenated as strings, but then evaluated as a number after being concatenated, and then converted again to give a string result.
That doesn't really make sense if you think of it. There is no reason that the string would be evaluated as a number, and besides, if it was evaluated as a number then the result would also be a number, not a string.
The number is converted to a string so that it can be concatenated to the other string, so the result is the same as for '0'+'1', which is the string '01'. There is no conversion to number and back to string after that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With