0

Why does eval(0047) return a value of 39? (047 and 00047 produce the same result)

And Why does eval(056) return a value of 46?

But...eval(0048) returns a value of 48, as expected!

It has something to do with the leading zeroes obviously, but I can't figure it out, any help would be appreciated.

2 Answers2

3

You don't need eval, you just need an integer literal beginning with 0 and where no digits are greater than 7 for JS to see that you're expressing an octal literal value. For example:

console.log(011) // 9

Similarly for hexadecimal:

console.log(0xcd) // 205

See also the Integers section on MDN.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
1

It's octal, base 8. The 4 is the "8's" place, not the "10's" place.

4 * 8 = 32 + 7 = 39.

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82