Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 032*2 = 52 in JavaScript? [duplicate]

I am interesting in understanding why 032*2 returns 52 in JavaScript.

I have a feeling that 032 could be interpenetrated as octal notation but I could not find a proper reference to it.

Could you please:

  • Provide me an explanation with references.

Thanks in advance your help on this.

like image 724
GibboK Avatar asked Nov 23 '25 10:11

GibboK


1 Answers

Numbers starting with 0 are Octal. So, 032 === 26.

To convert it into Base-10/Decimal number use parseInt with radix 10.

parseInt('032', 10) * 2; // 64

From MDN Docs:

If radix is undefined or 0 (or absent), JavaScript assumes the following:

  1. If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
  2. If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
  3. If the input string begins with any other value, the radix is 10 (decimal).
like image 59
Tushar Avatar answered Nov 25 '25 02:11

Tushar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!