3

I've been trying to debug a problem today and I finally found out what the problem is. For some reason, when a number has a leading zero JavaScript does something really weird.

Example:

alert(132);
alert(0132);

var test = 0132;
alert(test);

JSFiddle: http://jsfiddle.net/U8sFu/3/

The first popup says "132," the second "90," and the third says "90."

My question is, why does the number 0132 become the number 90? This is really baffling to me!

Nate
  • 26,164
  • 34
  • 130
  • 214

1 Answers1

6

A number with a leading 0 is parsed as an octal literal, which is in base 8.

David G
  • 94,763
  • 41
  • 167
  • 253
  • I did not know this... I was going to say that 90 is the octal equiv. of 0132. TIL. Thanks :) – brbcoding Apr 01 '13 at 18:02
  • Thank you! I thought I was losing my mind for a while there :-) Apparently this extremely unintuitive behavior is being removed in JavaScript 5: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/parseInt#ECMAScript_5_Removes_Octal_Interpretation – Nate Apr 01 '13 at 18:08